在Linq需要帮助,我需要将数据添加到List

时间:2012-02-10 11:16:45

标签: xml linq windows-phone-7

这是我的XML文件的结构。

<Resto>
<ID>2</ID>
<Name>name</Name>
<Category>categroty</Category>
<Places>
  <Address>
    <Location>loc</Location>
    <Number>num</Number>
    <Longitude>"empty"</Longitude>
    <Latitude>"empty"</Latitude>
  </Address>
</Places>
</Resto>

经度和纬度是空的,我现在不使用它们,这些都是为了以后的更新。 有些人可能有超过1个地址:

<Address>
 <Location>loc</Location>
    <Number>num</Number>
    <Longitude>"empty"</Longitude>
    <Latitude>"empty"</Latitude>
  </Address>

我也做了这个查询工作得非常好:

var anything = from resto in appDataXml.Descendants("Resto")

                     select new limit()
                     {
                         ID = resto.Element("ID").Value,
                         Name = resto.Element("Name").Value,
                         Categories = resto.Element("Category").Value
                     };

我在限制类中有这些:

    public string Name{get;set;}
    public string ID { get; set; }
    public string Categories{get;set;}
    public List<Address> Addresses { get; set; }

和“Address”是另一个具有Location和Number get / set的类。

无论如何,我的问题是如何查询Xml文件并将位置和数字添加到地址列表中,以便我可以将这些值添加到ListBox。

非常感谢。

2 个答案:

答案 0 :(得分:0)

如果您使用的是.net版本3.5,那么要使用XML数据,最好是用户LINQ to XML

http://www.codeproject.com/Articles/24376/LINQ-to-XML

Manipulate XML data with XPath and XmlDocument (C#)

答案 1 :(得分:0)

var doc = XDocument.Load("test.xml");
List<Address> locations = (from address in doc.Root.Element("Places").Elements("Address")
         select new Address
         {
              Location = address.Element("Location").Value,
              Number = address.Element("Number").Value,
         }).ToList();

确保您有根节点(如<parent>):

<parent>
  <ID>1</ID>
  <Name></Name>
  <Category></Category>
  <Places>
    <Address>
      <Location></Location>
      <Number></Number>
    </Address>
    <Address>
      <Location></Location>
      <Number></Number>
    </Address>
  </Places>
</parent>

<强>更新 如果你的xml看起来像这样:

<Restos>
  <Resto>
    <ID>1</ID>
    <Name>name</Name>
    <Category>categroty</Category>
    <Places>
      <Address>
        <Location>loc1</Location>
        <Number>num1</Number>
        <Longitude>"empty"</Longitude>
        <Latitude>"empty"</Latitude>
      </Address>
    </Places>
  </Resto>
  <Resto>
    <ID>2</ID>
    <Name>name</Name>
    <Category>categroty</Category>
    <Places>
      <Address>
        <Location>loc2</Location>
        <Number>num2</Number>
        <Longitude>"empty"</Longitude>
        <Latitude>"empty"</Latitude>
      </Address>
    </Places>
  </Resto>
</Restos>

您可以使用此代码解析它:

var doc = XElement.Load("test2.xml");
List<Resto> restos = (from resto in doc.Elements("Resto")
                        select new Resto
                        {
                            ID = resto.Element("ID").Value,
                            Name = resto.Element("Name").Value,
                            Category = resto.Element("Category").Value,
                            Addresses = (from address in resto.Element("Places").Elements("Address")
                                        select new Address
                                        {
                                            Location = address.Element("Location").Value,
                                            Number = address.Element("Number").Value,
                                        }).ToList()
                        }).ToList();

RestoAddress定义为:

public class Resto
{
    public string ID { get; set; }
    public string Name { get; set; }
    public string Category { get; set; }
    public List<Address> Addresses { get; set; }
}

public class Address
{
    public string Location { get; set; }
    public string Number { get; set; }
}