我有这种形式的 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/