C# 使用 XmlDocument 读取 XML 属性

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/933687/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 03:37:58  来源:igfitidea点击:

Read XML Attribute using XmlDocument

c#.netxmlxmldocument

提问by Alex

How can I read an XML attribute using C#'s XmlDocument?

如何使用 C# 的 XmlDocument 读取 XML 属性?

I have an XML file which looks somewhat like this:

我有一个看起来像这样的 XML 文件:

<?xml version="1.0" encoding="utf-8" ?>
<MyConfiguration xmlns="http://tempuri.org/myOwnSchema.xsd" SuperNumber="1" SuperString="whipcream">
    <Other stuff />
</MyConfiguration> 

How would I read the XML attributes SuperNumber and SuperString?

我将如何读取 XML 属性 SuperNumber 和 SuperString?

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?

目前我正在使用 XmlDocument,并且我使用 XmlDocument 获得了介于两者之间的值,GetElementsByTagName()并且效果非常好。我就是不知道如何获得属性?

采纳答案by Arsen Mkrtchyan

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

回答by jerryjvl

XmlDocument.Attributesperhaps? (Which has a method GetNamedItem that will presumably do what you want, although I've always just iterated the attribute collection)

XmlDocument.Attributes也许?(它有一个方法 GetNamedItem 大概可以做你想做的事,虽然我一直只是迭代属性集合)

回答by Greg

You should look into XPath. Once you start using it, you'll find its a lot more efficient and easier to code than iterating through lists. It also lets you directly get the things you want.

您应该查看XPath。一旦你开始使用它,你会发现它比遍历列表更高效、更容易编写代码。它还可以让您直接获得您想要的东西。

Then the code would be something similar to

然后代码将类似于

string attrVal = doc.SelectSingleNode("/MyConfiguration/@SuperNumber").Value;

Note that XPath 3.0 became a W3C Recommendation on April 8, 2014.

请注意,XPath 3.0 于 2014 年 4 月 8 日成为 W3C 推荐标准。

回答by Matt Sherman

You can migrate to XDocument instead of XmlDocument and then use Linq if you prefer that syntax. Something like:

如果您喜欢这种语法,您可以迁移到 XDocument 而不是 XmlDocument,然后使用 Linq。就像是:

var q = (from myConfig in xDoc.Elements("MyConfiguration")
         select myConfig.Attribute("SuperString").Value)
         .First();

回答by siva

I have an Xml File books.xml

我有一个 Xml 文件 books.xml

<ParameterDBConfig>
    <ID Definition="1" />
</ParameterDBConfig>

Program:

程序:

XmlDocument doc = new XmlDocument();
doc.Load("D:/siva/books.xml");
XmlNodeList elemList = doc.GetElementsByTagName("ID");     
for (int i = 0; i < elemList.Count; i++)     
{
    string attrVal = elemList[i].Attributes["Definition"].Value;
}

Now, attrValhas the value of ID.

现在,attrVal具有 的值ID

回答by Colonel Panic

Assuming your example document is in the string variable doc

假设您的示例文档在字符串变量中 doc

> XDocument.Parse(doc).Root.Attribute("SuperNumber")
1

回答by Voicu

If your XML contains namespaces, then you can do the following in order to obtain the value of an attribute:

如果您的 XML 包含命名空间,那么您可以执行以下操作以获得属性的值:

var xmlDoc = new XmlDocument();

// content is your XML as string
xmlDoc.LoadXml(content);

XmlNamespaceManager nsmgr = new XmlNamespaceManager(new NameTable());

// make sure the namespace identifier, URN in this case, matches what you have in your XML 
nsmgr.AddNamespace("ns", "urn:oasis:names:tc:SAML:2.0:protocol");

// get the value of Destination attribute from within the Response node with a prefix who's identifier is "urn:oasis:names:tc:SAML:2.0:protocol" using XPath
var str = xmlDoc.SelectSingleNode("/ns:Response/@Destination", nsmgr);
if (str != null)
{
    Console.WriteLine(str.Value);
}

More on XML namespaces hereand here.

更多关于 XML 命名空间的信息在这里这里