【问题标题】:How do I put XML into a List of Dictionaries with C# on Windows Phone 7如何在 Windows Phone 7 上使用 C# 将 XML 放入字典列表
【发布时间】:2011-12-13 22:33:51
【问题描述】:

这是我在文件中的 XML:

特别说明: 这是 Windows Phone 7 的问题,而不是一般的 C#

<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0">
    <item>
        <date>01/01</date>
        <word>aberrant</word>
        <def>straying from the right or normal way</def>
    </item>

    <item>
        <date>01/02</date>
        <word>Zeitgeist</word>
        <def>the spirit of the time.</def>
    </item>
</rss>

我需要它在 Dictionary 对象的 List(又名数组)中。每个Dictionary 代表一个&lt;item&gt;。像&lt;word&gt; 这样的每个元素都是key 类型为string 而像“Zeitgeist”这样的每个值都是value 类型为string

有什么简单的方法可以做到这一点吗?我来自 Objective-C 和 iOS,所以这对我来说是全新的 .NET 和 C#。

【问题讨论】:

    标签: c# .net xml visual-studio windows-phone-7


    【解决方案1】:

    LINQ-to-XML 让它变得非常简单。这是一个完整的例子:

            public static void Main(string[] args)
            {
                string xml = @"
    <rss version='2.0'>
        <item>
            <date>01/01</date>
            <word>aberrant</word>
            <def>straying from the right or normal way</def>
        </item>
    
        <item>
            <date>01/02</date>
            <word>Zeitgeist</word>
            <def>the spirit of the time.</def>
        </item>
    </rss>";
                var xdoc = XDocument.Parse(xml);
                var result = xdoc.Root.Elements("item")
                    .Select(itemElem => itemElem.Elements().ToDictionary(e => e.Name.LocalName, e => e.Value))
                    .ToList();
    
            }
    

    您可能会使用 XDocument.Load(filename) 而不是使用 XDocument.Parse() 从字符串加载,但无论哪种方式都可以使用 XDocument 对象(例如,我做了一个字符串)。

    【讨论】:

      【解决方案2】:

      您可以使用 Linq-Xml 来执行此操作:

      var doc = XDocument.Parse(xml); //xml is a String with your XML in it.
      doc
      .Root                         //Elements under the root element.
      .Elements("item")             //Select the elements called "item".
      .Select(                      //Projecting each item element to something new.
          item =>                   //Selecting each element in the item.
              item                  //And creating a new dictionary using the element name
              .Elements()           // as the key and element value as the value. 
              .ToDictionary(xe => xe.Name.LocalName, xe => xe.Value))
      .ToList();
      

      【讨论】:

      • 如何将 Linq 添加到我的项目中?添加 using System.Xml.Linq 给我一个错误,说它不在程序集中。感谢您的帮助。
      • 您将需要对 System.Xml.Linq 的引用。我不确定 WP7 是否可以做到这一点。
      • 是的,我正在尝试在 Windows Phone 7 上执行此操作。
      • 我没有 SDK,但快速 google 似乎表明这是可能的。在解决方案资源管理器中右键单击您的项目,然后单击“添加引用...”。单击“.Net”选项卡并查找 System.Xml.Linq。
      • 已修复...忘记添加对程序集的引用。
      【解决方案3】:

      是的,有一个简单的方法,叫做 LINQ to XML。

      一些资源:

      Parsing complex XML with C#

      LINQ to read XML

      http://msdn.microsoft.com/en-us/library/bb387098.aspx

      希望这会有所帮助...

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2012-11-15
        • 2011-08-01
        • 2023-03-16
        • 1970-01-01
        • 1970-01-01
        • 2011-08-04
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多