【问题标题】:Read XML Attribute using XmlDocument使用 XmlDocument 读取 XML 属性
【发布时间】:2010-10-30 08:10:12
【问题描述】:

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

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

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

如何读取 XML 属性 SuperNumber 和 SuperString?

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

【问题讨论】:

    标签: c# .net xml xmldocument


    【解决方案1】:

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

    【讨论】:

      【解决方案2】:
      XmlNodeList elemList = doc.GetElementsByTagName(...);
      for (int i = 0; i < elemList.Count; i++)
      {
          string attrVal = elemList[i].Attributes["SuperString"].Value;
      }
      

      【讨论】:

      • 非常感谢。它确实有效,它不需要任何路径,什么都不需要。简直太棒了!
      【解决方案3】:

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

      那么代码将类似于

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

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

      【讨论】:

        【解决方案4】:

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

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

        【讨论】:

          【解决方案5】:

          我有一个 XML 文件 books.xml

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

          程序:

          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;
          }
          

          现在,attrVal 的值为 ID。

          【讨论】:

            【解决方案6】:

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

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

            【讨论】:

              【解决方案7】:

              如果您的 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);
              }
              

              更多关于 XML 命名空间 here 和 here。

              【讨论】:

                猜你喜欢
                • 1970-01-01
                • 2013-08-28
                • 2015-05-18
                • 1970-01-01
                • 2012-10-20
                • 2021-06-03
                • 1970-01-01
                • 1970-01-01
                相关资源
                最近更新 更多