Windows Phone应用程序-C#linq解析xml [英] Windows phone App - C# linq to parse xml
本文介绍了Windows Phone应用程序-C#linq解析xml的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我如何解析.xml文件中的信息?
How do i parse information from a .xml file?
下面是我正在使用的.xml文件.
Below is the .xml file i am using.
如何获取名称值并将其存储在字符串中?
How do i get the name values and store them in Strings?
<query xmlns:yahoo="http://www.yahooapis.com/v1/base.rng" yahoo:count="10" yahoo:created="2012-10-23T01:49:32Z" yahoo:lang="en-US">
<diagnostics>
<publiclyCallable>true</publiclyCallable>
<url execution-start-time="1" execution-stop-time="202" execution-time="201">
<![CDATA[
http://where.yahooapis.com/v1/places.q(san%20francisco%2C%20ca);start=0;count=10
]]>
</url>
<user-time>203</user-time>
<service-time>201</service-time>
<build-version>31071</build-version>
</diagnostics>
<results>
<place xmlns="http://where.yahooapis.com/v1/schema.rng">
<name>San Francisco</name>
<country code="US" type="Country" woeid="23424977">United States</country>
</place>
<place xmlns="http://where.yahooapis.com/v1/schema.rng">
<name>San Francisco</name>
<country code="ES" type="Country" woeid="23424950">Spain</country>
</place>
<place xmlns="http://where.yahooapis.com/v1/schema.rng">
<name>San Francisco</name>
<country code="ES" type="Country" woeid="23424950">Spain</country>
</place>
<place xmlns="http://where.yahooapis.com/v1/schema.rng">
<name>San Francisco</name>
<country code="BO" type="Country" woeid="23424762">Bolivia</country>
</place>
<place xmlns="http://where.yahooapis.com/v1/schema.rng">
<name>San Francisco</name>
<country code="BO" type="Country" woeid="23424762">Bolivia</country>
</place>
<place xmlns="http://where.yahooapis.com/v1/schema.rng">
<name>San Francisco</name>
<country code="BO" type="Country" woeid="23424762">Bolivia</country>
</place>
<place xmlns="http://where.yahooapis.com/v1/schema.rng">
<name>San Francisco</name>
<country code="BO" type="Country" woeid="23424762">Bolivia</country>
</place>
<place xmlns="http://where.yahooapis.com/v1/schema.rng">
<name>San Francisco</name>
<country code="BO" type="Country" woeid="23424762">Bolivia</country>
</place>
<place xmlns="http://where.yahooapis.com/v1/schema.rng">
<name>San Francisco</name>
<country code="BO" type="Country" woeid="23424762">Bolivia</country>
</place>
<place xmlns="http://where.yahooapis.com/v1/schema.rng">
<name>San Francisco</name>
<country code="PE" type="Country" woeid="23424919">Peru</country>
</place>
</results>
</query>
推荐答案
您可以使用LINQ to XML来解析xml文件中的数据.
You can use LINQ to XML to parse the data from your xml file.
例如:
using System.Collections.Generic;
using System.Linq;
using Microsoft.Phone.Controls;
using System.Xml.Linq;
namespace PhoneApp1
{
public partial class MainPage : PhoneApplicationPage
{
public MainPage()
{
InitializeComponent();
// the xml file contains your provided xml code
XDocument xdoc1 = XDocument.Load("XMLFile1.xml");
XNamespace ns = XNamespace.Get("http://where.yahooapis.com/v1/schema.rng");
List<Place> places =
(from query in xdoc1.Elements("query").Elements("results").Elements(ns + "place")
select new Place
{
Name = query.Element(ns + "name") != null ?
(string)query.Element(ns + "name") : string.Empty,
Country = query.Element(ns + "country") != null ?
(string)query.Element(ns + "country") : string.Empty,
}).ToList();
List<string> names = new List<string>();
foreach (var item in places)
names.Add(item.Name);
}
}
public class Place
{
public string Name { get; set; }
public string Country { get; set; }
}
}
希望我能帮您解决这个小例子.
I hope I could help you with this small example.
这篇关于Windows Phone应用程序-C#linq解析xml的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文