1

我有非常简单的 Xml 结构,我想将其转换为对象列表。我的代码确实有效,但我认为这不是正确的方法,因为我从未这样做过,我认为可能有更简单的方法来做我想做的事。

xml 示例

<root>
    <item>
        <name>Item 1</name>
        <price>30.00</price>
    </item>
    <item>
        <name>Item 2</name>
        <price>55.00</price>
    </item>
</root>

收集 xml 并创建对象列表的代码

class Program
{
    static void Main(string[] args)
    {
        List<Item> itemList = new List<Item>();
        var url = "http://xmlurl.com/xml";

        // Load xml data
        XmlDocument myXmlDocument = new XmlDocument();
        myXmlDocument.Load(url);

        // Select items and loop
        var xmlItems = myXmlDocument.SelectNodes("/root/item");
        foreach (XmlNode item in xmlItems)
        {
            var newItem = new Item();
            foreach (XmlNode i in item)
            {
                // Since I cannot query them properly I need to check every item node
                switch (i.Name)
                {
                    case "name":
                        newItem.Name = i.InnerText;
                        break;
                    case "price":
                        newItem.Price = Convert.ToDecimal(i.InnerText);
                        break;
                }
            }
            itemList.Add(newItem);
        }

        // Test it out
        foreach (var item in itemList.OrderBy(x => x.Price))
        {
            Console.WriteLine(item.Name + " | " + item.Price);
        }
        Console.ReadLine();
    }
}

class Item
{
    public string Name { get; set; }
    public decimal Price { get; set; }
}
4

3 回答 3

1

使用 LINQ:

XDocument xdoc = XDocument.Load("myXml.xml");

List<Item> items = (from item in xdoc.Descendants("item")
                    select new Item {
                    Name = item.Element("name").Value,
                    Price = item.Element("price").Value
                    }).ToList();
于 2013-06-18T13:29:07.273 回答
0

您应该使用 XmlSerializer,例如:

课程:

  [XmlType(TypeName="item")]
  public class Item {
    public string Name { get; set; }
    public decimal Price { get; set; }
  }

  [XmlRoot(ElementName = "root")]
  public class ItemList : List<Item> {
  }

从标记中获取它们:

    const string test = @"<root>
    <item>
        <name>Item 1</name>
        <price>30.00</price>
    </item>
    <item>
        <name>Item 2</name>
        <price>55.00</price>
    </item>
</root>";

  var serializer = new XmlSerializer(typeof(ItemList));
  List<Item> result;
  using (var reader = new StringReader(test)) {
    result = (List<Item>)serializer.Deserialize(reader);
  }
于 2013-06-18T13:27:52.097 回答
0

你可以定义你的类:

[XmlType("item")]
public class Item
{
    [XmlElement("name")]
    public string Name { get; set; }

    [XmlElement("price")]
    public decimal Price { get; set; }
}

然后反序列化 Xml:

var xml = @"<root>
    <item>
        <name>Item 1</name>
        <price>30.00</price>
    </item>
    <item>
        <name>Item 2</name>
        <price>55.00</price>
    </item>
</root>";

List<Item> items;

var serializer = new XmlSerializer(typeof(List<Item>),
                                   new XmlRootAttribute("root"));

using(var stream = new StringReader(xml))
{
    items = (List<Item>)serializer.Deserialize(stream);
}

if(items != null)
{
    foreach(var item in items)
    {
        Console.Write(item);
    }
}
于 2013-06-18T13:33:36.177 回答