2013-11-01 56 views
0

XML元素我有這樣一個XML文件:如何獲得命名空間

我想所有的標籤:進入

<?xml version="1.0" encoding="utf-8"?> 
<a:feed xmlns:a="http://www.w3.org/2005/Atom" xmlns:os="http://a9.com/-/spec/opensearch/1.1/" xmlns="http://schemas.zune.net/catalog/apps/2008/02"> 
    <a:link rel="next" type="application/atom+xml" href="/v8/catalog/apps?q=facebook&amp;startIndex=50&amp;chunkSize=50&amp;pagingToken=50%7c50&amp;os=8.0.10512.0&amp;cc=US&amp;oc=&amp;lang=vi-VN&amp;hw=469838850&amp;dm=Virtual&amp;oemId=NOKIA&amp;moId=" /> 
    <a:link rel="self" type="application/atom+xml" href="/v8/catalog/apps?q=facebook&amp;chunkSize=50&amp;os=8.0.10512.0&amp;cc=US&amp;oc=&amp;lang=vi-VN&amp;hw=469838850&amp;dm=Virtual&amp;oemId=NOKIA&amp;moId=" /> 
    <os:startIndex>0</os:startIndex> 
    <os:totalResults>51</os:totalResults> 
    <os:itemsPerPage>50</os:itemsPerPage> 
    <a:updated>2013-11-01T08:30:11.711450Z</a:updated> 
    <a:title type="text">List Of Items</a:title> 
    <a:id>tag:catalog.zune.net,2013-11-01:/apps</a:id> 
    <pagingToken>50|50</pagingToken> 
    <impressionId>cd11c7b0116143dcb4c99f15b72ebbc4</impressionId> 
    <a:entry> 
    <a:updated>2013-11-01T08:30:11.711450Z</a:updated> 
    <a:title type="text">Facebook</a:title> 
    <a:id>urn:uuid:82a23635-5bd9-df11-a844-00237de2db9e</a:id> 
    <isHardwareCompatible>true</isHardwareCompatible> 
    <sortTitle>Facebook</sortTitle> 
    <releaseDate>2010-10-19T13:07:17.103000Z</releaseDate> 
    <version>5.1.2.0</version> 
    <averageUserRating>5.963479</averageUserRating> 
    <userRatingCount>42825</userRatingCount> 
    <image> 
     <id>urn:uuid:f8b42bcd-45c3-4ea5-9c9e-a108ac33cd6e</id> 
    </image> 
    <hasLiveTile>true</hasLiveTile> 
    <categories> 
     <category> 
     <id>windowsphone.Social</id> 
     <title>mạng xã hội</title> 
     <isRoot>True</isRoot> 
     </category> 
    </categories> 
    <tags> 
     <tag>Independent</tag> 
     <tag>LockScreen_Background</tag> 
     <tag>LockScreen_Notification_IconCount</tag> 
     <tag>LockScreen_Notification_TextField</tag> 
     <tag>phone.protocol.fb</tag> 
    </tags> 
    <offers> 
     <offer> 
     <offerId>urn:uuid:5c6b4028-74c5-4648-a71e-2ca413a5d2fd</offerId> 
     <mediaInstanceId>urn:uuid:64051c69-fb7b-4972-ad42-1dbb2e626a2c</mediaInstanceId> 
     <clientTypes> 
      <clientType>WindowsPhone80</clientType> 
      <clientType>WindowsPhone81</clientType> 
     </clientTypes> 
     <paymentTypes> 
      <paymentType>Credit Card</paymentType> 
      <paymentType>Mobile Operator</paymentType> 
     </paymentTypes> 
     <store>ZEST</store> 
     <price>0</price> 
     <displayPrice>$0.00</displayPrice> 
     <priceCurrencyCode>USD</priceCurrencyCode> 
     <licenseRight>Purchase</licenseRight> 
     </offer> 
    </offers> 
    <publisher> 
     <id>Microsoft Corporation</id> 
     <name>Microsoft Corporation</name> 
    </publisher> 
    <impressionId>cd11c7b0116143dcb4c99f15b72ebbc4</impressionId> 
    <k>4</k> 
    </a:entry> 

我的代碼:

var list = from r in xdoc.Descendants("a:entry") select r; 

而且我得到這個錯誤:

':'字符,十六進制值0x3A,不能包含在 名稱中。

我如何得到a:entry標籤?謝謝:)

+0

[Xml中的錯誤到列表代碼的可能的重複。 ':'字符,十六進制值0x3A,不能包含在名稱中](http://stackoverflow.com/questions/18213304/error-in-xml-to-list-code-the-character-hexadecimal-value- 0x3a-不可能) –

回答

2

你需要指定XNamespace

XNamespace xmlns = "http://www.w3.org/2005/Atom"; 

然後用XNamespace重載+操作,接受字符串:

var list = from r in xdoc.Descendants(xmlns + "entry") select r; 

甚至更​​好

var list = xdoc.Descendants(xmlns + "entry"); 

對於例如,爲了選擇所有標籤從第一entry使用下面的代碼爲例:

XNamespace entryNamespace = "http://www.w3.org/2005/Atom"; 
XNamespace tagNamespace = "http://schemas.zune.net/catalog/apps/2008/02"; 

var tags = xdoc.Descendants(entryNamespace + "entry") 
       .First() 
       .Descendants(tagNamespace + "tag"); 

foreach (var tag in tags) 
{ 
    Console.WriteLine (tag.Value); 
} 

打印:

Independent 
LockScreen_Background 
LockScreen_Notification_IconCount 
LockScreen_Notification_TextField 
phone.protocol.fb 

這裏是命名空間無關的版本(選擇相同tags):

var tags = xdoc.Descendants() 
       .Where(node => node.Name.LocalName == "entry") 
       .First() 
       .Descendants() 
       .Where(node => node.Name.LocalName == "tag"); 

,你當然可以始終選擇xml中的所有標記,而不指定名稱空間或entry節點名稱:

var tags = xdoc.Descendants() 
       .Where(node => node.Name.LocalName == "tag"); 
+0

精彩的回答,很好的工作,太多太多了:D –

+0

@IIya Ivanov:我可以問你的1個問題,我怎樣才能得到的innertext和的標記標記。 我試過了:var eVersion = item.Element(「version」);或者var eVersion = item.Element(a +「version」); ...不起作用 –

+0

@BuiAkinori和我使用標籤'.Descendants(entryNamespace +「tag」)做的一樣,但是它會像'.Descendants (entryNamespace +「version」)'或'.descendants(entryNamespace +「id」)' –