我是XPath的新手。我需要帮助解决在本XML结束时提取Book的标题和作者所需的XPath。我尝试了以下C#代码但没有成功。我只需要列出本书的标题和作者。看起来xmlns名称空间会影响我的XPath。如果我手动删除xmlns,我的代码可以正常工作。因此,要么修改XPath以考虑此命名空间,要么找出从XML中删除该属性的方法。请指教。
这是C#代码:
XmlNodeList nodes = XML.DocumentElement.SelectNodes("//Title");
foreach (XmlNode node in nodes)
{
Console.WriteLn(node.Name + " = " + node.InnerText); }
}
这是XML:
<?xml version="1.0"?>
<ItemSearchResponse xmlns="http://webservices.amazon.com/AWSECommerceService/2011-08-01">
<OperationRequest>
<RequestId>xxxxx</RequestId>
<Arguments>
<Argument Name="Condition" Value="All"></Argument>
<Argument Name="ResponseGroup" Value="Small,Images"></Argument>
<Argument Name="SearchIndex" Value="Books"></Argument>
</Arguments>
<RequestProcessingTime>0.0735170000000000</RequestProcessingTime>
</OperationRequest>
<Items>
<Request>
<IsValid>True</IsValid>
<ItemSearchRequest>
<Condition>All</Condition>
<Keywords>Perl</Keywords>
<ResponseGroup>Small</ResponseGroup>
<ResponseGroup>Images</ResponseGroup>
<SearchIndex>Books</SearchIndex>
</ItemSearchRequest>
</Request>
<TotalResults>3761</TotalResults>
<TotalPages>377</TotalPages>
<MoreSearchResultsUrl>http://www.amazon.com/gp/redirect.html?camp=2025&creative=386001&location=http%3A%2F%2Fwww.amazon.com%2Fgp%2Fsearch%3Fkeywords%3DPerl%26url%3Dsearch-alias%253Dstripbooks&linkCode=xm2&tag=geo01d-20&SubscriptionId=AKIAJJBQEKP2X72RQ6XA</MoreSearchResultsUrl>
<Item>
<ASIN>1449303587</ASIN>
<DetailPageURL>http://www.amazon.com/Learning-Perl-Randal-L-Schwartz/dp/1449303587%3FSubscriptionId%3DAKIAJJBQEKP2X72RQ6XA%26tag%3Dgeo01d-20%26linkCode%3Dxm2%26camp%3D2025%26creative%3D165953%26creativeASIN%3D1449303587</DetailPageURL>
<ItemLinks>
<ItemLink>
<Description>Technical Details</Description>
<URL>http://www.amazon.com/Learning-Perl-Randal-L-Schwartz/dp/tech-data/1449303587%3FSubscriptionId%3DAKIAJJBQEKP2X72RQ6XA%26tag%3Dgeo01d-20%26linkCode%3Dxm2%26camp%3D2025%26creative%3D386001%26creativeASIN%3D1449303587</URL>
</ItemLink>
<ItemLink>
<Description>All Offers</Description>
<URL>http://www.amazon.com/gp/offer-listing/1449303587%3FSubscriptionId%3DAKIAJJBQEKP2X72RQ6XA%26tag%3Dgeo01d-20%26linkCode%3Dxm2%26camp%3D2025%26creative%3D386001%26creativeASIN%3D1449303587</URL>
</ItemLink>
</ItemLinks>
<SmallImage>
<URL>http://ecx.images-amazon.com/images/I/51kTNE8aIXL._SL75_.jpg</URL>
<Height Units="pixels">75</Height>
<Width Units="pixels">58</Width>
</SmallImage>
<MediumImage>
<URL>http://ecx.images-amazon.com/images/I/51kTNE8aIXL._SL160_.jpg</URL>
<Height Units="pixels">160</Height>
<Width Units="pixels">123</Width>
</MediumImage>
<LargeImage>
<URL>http://ecx.images-amazon.com/images/I/51kTNE8aIXL.jpg</URL>
<Height Units="pixels">500</Height>
<Width Units="pixels">385</Width>
</LargeImage>
<ImageSets>
<ImageSet Category="primary">
<SwatchImage>
<URL>http://ecx.images-amazon.com/images/I/51kTNE8aIXL._SL30_.jpg</URL>
<Height Units="pixels">30</Height>
<Width Units="pixels">23</Width>
</SwatchImage>
<SmallImage>
<URL>http://ecx.images-amazon.com/images/I/51kTNE8aIXL._SL75_.jpg</URL>
<Height Units="pixels">75</Height>
<Width Units="pixels">58</Width>
</SmallImage>
</ImageSet>
</ImageSets>
<ItemAttributes>
<Author>Randal L. Schwartz</Author>
<Author>brian d foy</Author>
<Author>Tom Phoenix</Author>
<Manufacturer>O'Reilly Media</Manufacturer>
<ProductGroup>Book</ProductGroup>
<Title>Learning Perl</Title>
</ItemAttributes>
</Item>
</Items>
</ItemSearchResponse>
答案 0 :(得分:1)
您可以使用Linq-to-XML执行此操作:
XDocument xmlDoc = XDocument.Parse(xmlString);
var q = from el in xmlDoc.Descendants()
.Where(x => x.Name.LocalName == "Title" || x.Name.LocalName == "Author")
select el;
foreach (var xElement in q)
{
Debug.WriteLine(xElement.Name.LocalName + " : " + xElement.Value);
}
输出:
Author : Randal L. Schwartz
Author : brian d foy
Author : Tom Phoenix
Title : Learning Perl
答案 1 :(得分:0)
XPath需要是&#34; // p:Title&#34;,你需要告诉XPath处理器命名空间前缀&#34; p&#34;代表命名空间&#34; http://webservices.amazon.com/AWSECommerceService/2011-08-01&#34;。建立命名空间绑定的方式取决于所选XPath处理器的API。对于C#,您可以在此处找到解释(或者在许多其他以前的StackOverflow答案中):
Xml-SelectNodes with default-namespace via XmlNamespaceManager not working as expected
答案 2 :(得分:0)
我终于想出了我的问题的答案。我只是删除了过分复杂我的XPath的命名空间。下面的C#代码有效。 PrintKeyValue只打印Key = Value。
XML = new XmlDocument();
using (XmlTextReader reader = new XmlTextReader("C:\\Path\\File.xml"))
{
reader.Namespaces = false;
XML.Load(reader);
}
XmlNodeList items = XML.DocumentElement.SelectNodes("//Item");
foreach (XmlNode item in items)
{
PrintKeyValue("ISBN10", item["ASIN"].InnerText);
XmlNode attrib = item.SelectSingleNode(".//ItemAttributes");
PrintKeyValue("Title", attrib["Title"].InnerText);
XmlNodeList authors = attrib.SelectNodes(".//Author");
StringBuilder sb = new StringBuilder();
int count = 0;
foreach (XmlNode author in authors)
{
count++;
if (count > 1) { sb.Append(", "); }
sb.Append(author.InnerText);
}
PrintKeyValue("Authors", sb.ToString());
}