c# - 获取 json 元素的标签及其 id

标签 c# javascript jquery json

我有这种形式的 json 元素;

<rect style="fill: #888888; display: inline;" id="17" width="35.823246" height="35.823246" x="456.61066" y="65.9505" class="seatObj" label="A18"></rect>

如何获取属性 label 值?

假设 <rect .../> 标记是 xml 的一部分,那么如何使用 C# 控制台应用程序获得相同的结果?

最佳答案

试试这个:

var elem = document.getElementsById('17');
var label = elem.getAttribute('label');
alert(label);

使用 jQuery:

alert($('#17').attr('label'));

您有 300 个这样的元素:

然后试试这个:

$('rect').each(function(){
     alert($(this).attr('label'));
});

这是 Demo

另一种方法是在 rect 元素中添加 class 属性,然后使用该类选择它们。 我添加了 class="sample" rect 元素。 检查这个 Fiddle

 $('.sample').each(function(){
     alert($(this).attr('label'));
 });

示例 xml 文件。

 <?xml version="1.0" encoding="utf-8" ?>
 <Test>
      <rect style="fill: #888888; display: inline;" id="17" width="35.823246" height="35.823246" x="456.61066" y="65.9505" class="seatObj" label="A18"></rect>
      <rect style="fill: #888888; display: inline;" id="18" width="35.823246" height="35.823246" x="456.61066" y="65.9505" class="seatObj" label="A19"></rect>
 </Test>

使用 C# 控制台应用程序解析 xml:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;

namespace Sample
{
    class Program
    {
        static void Main(string[] args)
        {
            XmlDocument doc = new XmlDocument();
            doc.Load("Url for Sample.xml");

            XmlNodeList elemList = doc.GetElementsByTagName("rect");
            for (int i = 0; i < elemList.Count; i++)
            {
                string attrVal = elemList[i].Attributes["label"].Value;
                Console.WriteLine(attrVal);
            }
            Console.ReadLine();
        }
    }
}

关于c# - 获取 json 元素的标签及其 id,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19535191/

相关文章:

c# - 将带有非托管导出的 C# DLL 中的字符串返回到 Inno Setup 脚本

javascript - 单击页面关闭弹出窗口上的任意位置 - 放大弹出窗口

javascript - 关闭菜单子(monad)类别保持先前的 div 打开

javascript - 始终只显示最近 10 条服务器发送的事件消息

c# - ASP.NET Core 2+ 中的 ILogger 和 DependencyInjection

c# - 上传大文件asp.net

c# - 我应该如何在 Windows Phone 7 上使用 RestSharp 实现 ExecuteAsync?

javascript - 当一个列表包含一个 'Button' 元素时,我怎么能只找到列表的值,但是按钮

javascript - 奇怪的 jQuery Ajax 问题

javascript - 放大和缩小选项未出现