如何读取xml属性值 [英] how to read xml attribute values
本文介绍了如何读取xml属性值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个xml,我想读取属性值,该怎么做?
xml:
Hi,
i have a xml and i want to read the attribute values, how do i do that?
xml:
<?xml version="1.0" encoding="utf-8"?>
<ContentMap tag="sdsdsadsadsadasdsadas">
<ContentNode type="BusinessProcess" name="MainProcess" url="mainbprocess.svg" tag="c2532e51-704f-45e9-abc7-f8a2a7b1f422">
<ContentNode type="BusinessProcess" name="Sub Bus Process1" url="subbprocess1.svg" tag="c2532e51-704f-45e9-abc7-f8a2a7b1f423">
<ContentNode type="ApplicationProcess" name="Sub App Process1" url="subappprocess1.svg" tag="c2532e51-704f-45e9-abc7-f8a2a7b1f424">
<ContentNode type="ReusableApplicationComponent" name="Sub App Component1" url="subappcomponent1.html" tag="c2532e51-704f-45e9-abc7-f8a2a7b1f427">
</ContentNode>
<ContentNode type="CustomApplicationComponent" name="Sub App Component2" url="subappcomponent2.html" tag="c2532e51-704f-45e9-abc7-f8a2a7b1f428">
</ContentNode>
</ContentNode>
</ContentNode>
<ContentNode type="ApplicationProcess" name="Sub App Process2" url="subappprocess2.svg" tag="c2532e51-704f-45e9-abc7-f8a2a7b1f425">
<ContentNode type="ReusableApplicationComponent" name="Sub App Component3" url="subappcomponent3.html" tag="c2532e51-704f-45e9-abc7-f8a2a7b1f429">
</ContentNode>
</ContentNode>
</ContentNode>
</ContentMap>
推荐答案
您需要使用XMLReader来读取XML.检查以下代码.
You need to use XMLReader to read the XML. check following code.
System.Xml.XmlReader xmlReader = System.Xml.XmlReader.Create(\\XML_File_path);
while(xmlReader.Read())
{
if(XmlNodeType.Attribute)
{
//get AttributeName and Value
Response.Write(xmlReader.Name.ToString()+ ": " + xmlReader.Value.ToString()));
}
}
尝试
XmlNodeList elemList = doc.GetElementsByTagName(...);
for (int i = 0; i < elemList.Count; i++)
{
string attrVal = elemList[i].Attributes["AttributeName"].Value;
}
或者您也可以尝试
XmlTextReader:读取所有属性 [读取C#中的XML节点属性数据 [
or you can also try
XmlTextReader: read all attributes[^]
Read XML node attributes data in C#[^]
一种简单得多的方法:
a much simpler way:
foreach (XmlNode n1 in list)
{
string str = n1.Attributes["name"].Value;
}
这个字符串 str 将具有每个节点的属性 name 的值,同样,只需给出您想要的属性的名称,我们就可以访问该值.这么简单:)
this string str will have the value of the attribute name of each node, likewise just give the name of the attribute u want and we can access the value.so simple :)
这篇关于如何读取xml属性值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文