【问题标题】:Reading child nodes from xml string using C#, LINQ使用 C#、LINQ 从 xml 字符串中读取子节点
【发布时间】:2015-11-24 08:46:36
【问题描述】:
- <entry xml:base="http://testserver.windows.net/" xmlns="http://www.w3.org/2005/Atom" xmlns:d="http://schemas.microsoft.com/ado/2007/08/dataservices" xmlns:m="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata" m:etag="W/"datetime'2015-08-30T00%3A04%3A02.9193525Z'"">
  <id>http://testserver.windows.net/Players(PartitionKey='zzz',RowKey='000125')</id> 
  <category term="testServer.Players" scheme="http://schemas.microsoft.com/ado/2007/08/dataservices/scheme" /> 
  <link rel="edit" title="Players" href="Players(PartitionKey='zzz',RowKey='000125')" /> 
  <title /> 
  <updated>2014-04-30T00:53:42Z</updated> 
- <author>
  <name /> 
  </author>
- <content type="application/xml">
- <m:properties>
  <d:PartitionKey>zzz</d:PartitionKey> 
  <d:RowKey>000125</d:RowKey> 
  <d:Timestamp m:type="Edm.DateTime">2014-04-30T00:04:02.9193525Z</d:Timestamp> 
  <d:Name>Black color</d:Name> 
  <d:Comments>Test comments</d:Comments> 
  </m:properties>
  </content>
  </entry>

如何使用 C# 或 LINQ 读取“m:properties”后代。 此 xml 字符串存储在 XElement 类型的变量中

【问题讨论】:

    标签: c# linq c#-4.0 xml-parsing linq-to-xml


    【解决方案1】:

    我想这可以告诉你如何使用 Linq to XML

    read the data from XML Structure using c#

    如果还有什么问题,只需稍微调试一下,看看你从 L2X 操作中得到了什么,然后在数据树中移动更深一步。

    【讨论】:

      【解决方案2】:

      使用 Linq2XML

      var xDoc = XDocument.Load(filename);
      var dict = xDoc.Descendants("m:properties")
                 .First()
                 .Attributes()
                 .ToDictionary(x => x.Name, x => x.Value);
      

      【讨论】:

        【解决方案3】:
        1. 设置命名空间管理器。请注意,.net 库不支持默认命名空间,因此我在默认命名空间中添加了前缀“ns”。

        2. 使用 xpath 或 linq 查询 xml。以下示例使用 xpath。

                      XmlNamespaceManager NamespaceManager = new XmlNamespaceManager(new NameTable());
                  NamespaceManager.AddNamespace("base", "http://testserver.windows.net/");
                  NamespaceManager.AddNamespace("d", "http://schemas.microsoft.com/ado/2007/08/dataservices");
                  NamespaceManager.AddNamespace("m", "http://schemas.microsoft.com/ado/2007/08/dataservices/metadata");
                  NamespaceManager.AddNamespace("ns", "http://www.w3.org/2005/Atom"); XDocument doc = XDocument.Parse(XElement);
          
              var properties = doc.XPathSelectElement("/ns:entry/ns:content/m:properties", NamespaceManager);
          

        【讨论】:

          【解决方案4】:

          您可以使用XNamespace+"element local name" 的组合来引用命名空间中的元素,例如:

          XElement myxelement = XElement.Parse("your XML string here");
          XNamespace m = "http://schemas.microsoft.com/ado/2007/08/dataservices/metadata";
          List<XElement> properties = myxelement.Descendants(m+"properties").ToList();
          

          【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2012-05-29
          相关资源
          最近更新 更多