问题
I have a XML file like this :
I want to get all the tag a:entry
<?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&startIndex=50&chunkSize=50&pagingToken=50%7c50&os=8.0.10512.0&cc=US&oc=&lang=vi-VN&hw=469838850&dm=Virtual&oemId=NOKIA&moId=" />
<a:link rel="self" type="application/atom+xml" href="/v8/catalog/apps?q=facebook&chunkSize=50&os=8.0.10512.0&cc=US&oc=&lang=vi-VN&hw=469838850&dm=Virtual&oemId=NOKIA&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>
My code :
var list = from r in xdoc.Descendants("a:entry") select r;
And i got this error :
The ':' character, hexadecimal value 0x3A, cannot be included in a name.
How I can get the a:entry tag ?? Thanks :)
回答1:
you need to specify XNamespace
:
XNamespace xmlns = "http://www.w3.org/2005/Atom";
and then use XNamespace
overloaded +
operator, that accepts string:
var list = from r in xdoc.Descendants(xmlns + "entry") select r;
or even better
var list = xdoc.Descendants(xmlns + "entry");
For example, in order to select all tags from First entry
use next code as an example:
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);
}
prints:
Independent
LockScreen_Background
LockScreen_Notification_IconCount
LockScreen_Notification_TextField
phone.protocol.fb
here is the namespace agnostic version (selects the same tags
):
var tags = xdoc.Descendants()
.Where(node => node.Name.LocalName == "entry")
.First()
.Descendants()
.Where(node => node.Name.LocalName == "tag");
and you of course can always select all the tags in the xml without specifying neither namespace nor entry
node name:
var tags = xdoc.Descendants()
.Where(node => node.Name.LocalName == "tag");
来源:https://stackoverflow.com/questions/19725310/how-to-get-xml-element-with-namespace