Xml 需要Linq方面的帮助,我需要将数据添加到列表中
这是我的XML文件的结构Xml 需要Linq方面的帮助,我需要将数据添加到列表中,xml,linq,windows-phone-7,Xml,Linq,Windows Phone 7,这是我的XML文件的结构 <Resto> <ID>2</ID> <Name>name</Name> <Category>categroty</Category> <Places> <Address> <Location>loc</Location> <Number>num</Number> <Longitud
<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>
我在极限类中有这些:
public string Name{get;set;}
public string ID { get; set; }
public string Categories{get;set;}
public List<Address> Addresses { get; set; }
公共字符串名称{get;set;}
公共字符串ID{get;set;}
公共字符串类别{get;set;}
公共列表地址{get;set;}
“Address”是另一个带有位置和编号get/set的类
无论如何,我的问题是如何查询Xml文件并将位置和编号添加到地址列表中,以便将这些值添加到列表框中
非常感谢。如果您使用的是.net 3.5版,那么使用XML数据对用户来说更好 或
我按照你说的做了,但是我得到了一个nullReferenceException。知道吗?@user1200204您是否将此代码应用于其他(可能更大)xml文件?你能用更完整的xml结构更新你的问题吗?@user1200204更新了答案
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; }
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>
<ID>1</ID>
<Name></Name>
<Category></Category>
<Places>
<Address>
<Location></Location>
<Number></Number>
</Address>
<Address>
<Location></Location>
<Number></Number>
</Address>
</Places>
</parent>
<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();
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; }
}