C#代码来搜索并导致xml

问题描述:

我在一些C#代码后,将有以下方法,将返回Xml作为结果。C#代码来搜索并导致xml

搜索Apple iTunes App商店。如果我传递了一个名称或部分名称,函数必须返回可能的搜索结果列表,或者只有一个结果是完美匹配的。如下图所示

例子:

<App> 

    <AppId>321564880</AppId> 

    <Name>Doodle Clock - Clock A Doodle Do!</Name> 

    <ReleaseDate>Released Sep 28, 2009</ReleaseDate> 

    <Artist>YARG</Artist> 

    <Description>Description of App</Description> 

    <Copyright>© YARG Limited 2009</Copyright> 

    <Price>$0.99</Price> 

    <Category>Lifestyle</Category> 

    <MainImageUrl><!—main App icon image urlà </ImageUrl> 

    <ExtraImages> 

     <!-- these will be the extra images you see in the App store other than the main application icon --> 

     <ImageUrl> <!—url of extra image 1à</ImageUrl> 

       <ImageUrl> <!—url of extra image 2à</ImageUrl> 

       <ImageUrl> <!—url of extra image 3à</ImageUrl> 

    </ExtraImages> 

    <Version>Version: 1.1 (iPhone OS 3.0 Tested)</Version> 

    <Size>1.5 MB</Size> 

</App> 
+0

你可以提供我们在编码尝试做到这一点吗?我不想听起来很粗鲁,但是SO不是一个编码机器...... – Philippe 2009-10-28 07:58:12

好的,创建xml文件并解析和处理它们的最好方法是使用XDocument,XElement等。因为它们是可枚举的,这意味着你可以在它们上使用LINQ,这会对你有很大的帮助。

例如:

XElement element = new XElement("Persons", 
           new XElement("Person","John", 
              new XAttribute("Id","1")), 
           new XElement("Person","Aaron", 
              new XAttribute("Id",2)) 
           ) 

回报

<Persons> 
<Person Id="1">John</Person> 
<Person Id="2">Aaron</Person> 
</Person> 

的更多信息:System.Xml.Linq Namespace

如果你正在寻找的速度,那么你可以使用XMLReaderXMLWriter,但你不能找到System.Xml.Linq提供的灵活性。

+0

你能提供一个完整的C#功能吗? – SmartestVEGA 2009-10-28 08:14:42

+0

@SmartestVEGA,我想但是我认为你要找的是一个动态列表,所以无论我在上面写什么,这都是没有意义的。 – Tarik 2009-10-28 08:19:07

+0

请证实亚伦! – SmartestVEGA 2009-10-28 08:26:42

+0

你能否给我一个样本脚本!? – SmartestVEGA 2009-10-28 07:51:14

+0

你能给我一些基础知识吗? – SmartestVEGA 2009-10-28 07:51:46

我会用XmlTextReader。这是最快的方式(尽管只是只读),如果你可能正在寻找速度。如果不是,XPath应该这样做。