使用 XmlDocument 读取 XML 属性 [英] Read XML Attribute using XmlDocument
本文介绍了使用 XmlDocument 读取 XML 属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何使用 C# 的 XmlDocument 读取 XML 属性?
How can I read an XML attribute using C#'s XmlDocument?
我有一个看起来像这样的 XML 文件:
I have an XML file which looks somewhat like this:
<?xml version="1.0" encoding="utf-8" ?>
<MyConfiguration xmlns="http://tempuri.org/myOwnSchema.xsd" SuperNumber="1" SuperString="whipcream">
<Other stuff />
</MyConfiguration>
如何读取 XML 属性 SuperNumber 和 SuperString?
How would I read the XML attributes SuperNumber and SuperString?
目前我正在使用 XmlDocument,我使用 XmlDocument 的 GetElementsByTagName()
获取介于两者之间的值,而且效果非常好.我就是不知道如何获得属性?
Currently I'm using XmlDocument, and I get the values in between using XmlDocument's GetElementsByTagName()
and that works really well. I just can't figure out how to get the attributes?
推荐答案
XmlNodeList elemList = doc.GetElementsByTagName(...);
for (int i = 0; i < elemList.Count; i++)
{
string attrVal = elemList[i].Attributes["SuperString"].Value;
}
这篇关于使用 XmlDocument 读取 XML 属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文