This is my XML snippet (it has a root element).
<ItemAttributes>
<Author>Ellen Galinsky</Author>
<Binding>Paperback</Binding>
<Brand>Harper Paperbacks</Brand>
<EAN>9780061732324</EAN>
<EANList>
<EANListElement>9780061732324</EANListElement>
</EANList>
<Edition>1</Edition>
<Feature>ISBN13: 9780061732324</Feature>
<Feature>Condition: New</Feature>
<Feature>Notes: BRAND NEW FROM PUBLISHER! 100% Satisfaction Guarantee. Tracking provided on most orders. Buy with Confidence! Millions of books sold!</Feature>
<ISBN>006173232X</ISBN>
<IsEligibleForTradeIn>1</IsEligibleForTradeIn>
<ItemDimensions>
<Height Units="hundredths-inches">112</Height>
<Length Units="hundredths-inches">904</Length>
<Weight Units="hundredths-pounds">98</Weight>
<Width Units="hundredths-inches">602</Width>
</ItemDimensions>
<Label>William Morrow Paperbacks</Label>
<ListPrice>
<Amount>1699</Amount>
<CurrencyCode>USD</CurrencyCode>
<FormattedPrice>$16.99</FormattedPrice>
</ListPrice>
<Manufacturer>William Morrow Paperbacks</Manufacturer>
<MPN>006173232X</MPN>
<NumberOfItems>1</NumberOfItems>
<NumberOfPages>400</NumberOfPages>
<PackageDimensions>
<Height Units="hundredths-inches">120</Height>
<Length Units="hundredths-inches">880</Length>
<Weight Units="hundredths-pounds">95</Weight>
<Width Units="hundredths-inches">590</Width>
</PackageDimensions>
<PartNumber>006173232X</PartNumber>
<ProductGroup>Book</ProductGroup>
<ProductTypeName>ABIS_BOOK</ProductTypeName>
<PublicationDate>2010-04-20</PublicationDate>
<Publisher>William Morrow Paperbacks</Publisher>
<ReleaseDate>2010-04-20</ReleaseDate>
<SKU>mon0000013657</SKU>
<Studio>William Morrow Paperbacks</Studio>
<Title>Mind in the Making: The Seven Essential Life Skills Every Child Needs</Title>
</ItemAttributes>
There are multiple "ItemAttributes" nodes, each having a different "ProductGroup" node. I want only the first "ItemAttribute" where "ProductGroup" = "book:"
This is my C# code:
XPathDocument doc = new XPathDocument(sr);
XPathNavigator nav = doc.CreateNavigator();
// Compile a standard XPath expression
XPathExpression expr;
expr = nav.Compile("//ItemAttributes[contains(ProductGroup, 'Book')]");
XPathNodeIterator iterator = nav.Select(expr);
// Iterate on the node set
try {
int x = iterator.Count; // <----------- count = 0
while (iterator.MoveNext()) { // <----------- finds nothing!
XPathNavigator nav2 = iterator.Current.Clone();
listBox1.Items.Add("price: " + nav2.Value);
}
}
catch (Exception ex) {
Console.WriteLine(ex.Message);
}
I know my code isn't correct, but I don't understand why the iterator.Count is zero!
using System.Xml.Linq
XDocument xdoc = XDocument.Load(new StringReader(xmlstr));
var foundNode = xdoc
.Descendants("ItemAttributes")
.Where(node => node.Element("ProductGroup").Value == "Book")
.First();
var price = foundNode.Element("ListPrice").Element("FormattedPrice").Value;
--EDIT--
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;
using System.IO;
namespace ConsoleApplication4
{
class Program
{
static void Main(string[] args)
{
string xmlstr = #"
<root>
<ItemAttributes>
<Author>Ellen Galinsky</Author>
<Binding>Paperback</Binding>
<Brand>Harper Paperbacks</Brand>
<EAN>9780061732324</EAN>
<EANList>
<EANListElement>9780061732324</EANListElement>
</EANList><Edition>1</Edition>
<Feature>ISBN13: 9780061732324</Feature>
<Feature>Condition: New</Feature>
<Feature>Notes: BRAND NEW FROM PUBLISHER! 100% Satisfaction Guarantee. Tracking provided on most orders. Buy with Confidence! Millions of books sold!</Feature>
<ISBN>006173232X</ISBN>
<IsEligibleForTradeIn>1</IsEligibleForTradeIn>
<ItemDimensions>
<Height Units=""hundredths-inches"">112</Height>
<Length Units=""hundredths-inches"">904</Length>
<Weight Units=""hundredths-pounds"">98</Weight>
<Width Units=""hundredths-inches"">602</Width>
</ItemDimensions>
<Label>William Morrow Paperbacks</Label>
<ListPrice>
<Amount>1699</Amount>
<CurrencyCode>USD</CurrencyCode>
<FormattedPrice>$16.99</FormattedPrice>
</ListPrice>
<Manufacturer>William Morrow Paperbacks</Manufacturer>
<MPN>006173232X</MPN>
<NumberOfItems>1</NumberOfItems>
<NumberOfPages>400</NumberOfPages>
<PackageDimensions>
<Height Units=""hundredths-inches"">120</Height>
<Length Units=""hundredths-inches"">880</Length>
<Weight Units=""hundredths-pounds"">95</Weight>
<Width Units=""hundredths-inches"">590</Width>
</PackageDimensions>
<PartNumber>006173232X</PartNumber>
<ProductGroup>Book</ProductGroup>
<ProductTypeName>ABIS_BOOK</ProductTypeName>
<PublicationDate>2010-04-20</PublicationDate>
<Publisher>William Morrow Paperbacks</Publisher>
<ReleaseDate>2010-04-20</ReleaseDate>
<SKU>mon0000013657</SKU>
<Studio>William Morrow Paperbacks</Studio>
<Title>Mind in the Making: The Seven Essential Life Skills Every Child Needs</Title>
</ItemAttributes>
</root>
";
XDocument xdoc = XDocument.Load(new StringReader(xmlstr));
var foundNode = xdoc
.Descendants("ItemAttributes")
.Where(node => node.Element("ProductGroup").Value == "Book")
.First();
Console.WriteLine(foundNode.Element("ListPrice").Element("FormattedPrice").Value);
Console.ReadLine();
}
}
}
--EDIT2--
XDocument xdoc = XDocument.Load("http://ecs.amazonaws.com/onca/xml?AWSAccessKeyId=AKIAIAAFYAPOR6SX5GOA&AssociateTag=pragbook-20&IdType=ISBN&ItemId=9780061732324&MerchantId=All&Operation=ItemLookup&ResponseGroup=Medium&SearchIndex=Books&Service=AWSECommerceService&Timestamp=2012-02-26T20%3A18%3A37Z&Version=2011-08-01&Signature=r7yE7BQI44CqWZAiK%2FWumF3N4iutOj3re9wZtESOaKs%3D");
XNamespace ns = XNamespace.Get("http://webservices.amazon.com/AWSECommerceService/2011-08-01");
var foundNode = xdoc
.Descendants(ns+"ItemAttributes")
.Where(node => node.Element(ns+"ProductGroup").Value == "Book")
.First();
Console.WriteLine(foundNode.Element(ns+"ListPrice").Element(ns+"FormattedPrice").Value);
Console.ReadLine();
I'd use XPath with XmlDocument to handle this.
XmlDocument xDoc = new XmlDocument();
xDoc.LoadXml(myXMLString);
XmlNodeList nodeList = xDoc.SelectNodes("//ItemAttributes[./ProductGroup[text()='Book']]");
foreach (XmlNode node in nodeList)
{
//Do anything with node.Value;
}
I haven't tried your code but from what I see your XPath expression was incorrect.
I have broken down the expression I wrote above, below.
It reads as
//ItemAttributes #look for all nodes named ItemAttributes
[
./ProductGroup #with a child node called ProductGroup
[
text()='Book' #that has the string 'Book' as the text
]
]
You say that your XML is a snippet. I'm going to hazard a guess that it's contained within an element that binds the default namespace prefix to a non-trivial URI, and that this is why you're getting no results from your iterator.
Your code worked for me, with the XML document as given above. I ran the code and got one element out of the iterator. I then took your XML document and wrapped it in a root element that binds the default namespace prefix to a made-up but non-trivial URI:
<SomeRootElement xmlns="http://schemas.blahblahblah.com/example">
<ItemAttributes>
<!-- rest of your document omitted -->
</ItemAttributes>
</SomeRootElement>
I then got no results from the iterator.
I then created an XmlNamespaceManager which mapped a prefix (I chose pfx) to the namespace URI used above:
XmlNamespaceManager mgr = new XmlNamespaceManager(new NameTable());
mgr.AddNamespace("pfx", "http://schemas.blahblahblah.com/example");
I then set this namespace manager as the namespace context for the XPath expression, and added the prefix pfx to the names within your XPath expression:
XPathExpression expr;
expr = nav.Compile("//pfx:ItemAttributes[contains(pfx:ProductGroup, 'Book')]");
expr.SetContext(mgr);
XPathNodeIterator iterator = nav.Select(expr);
I then got the one element out of the iterator, as expected.
XPath can be a bit funny with namespaces. I tried binding the empty prefix "" to the URI so that I could use your XPath expression unmodified, but that didn't work. That's one thing I've found with XPath before: always bind namespace URIs to prefixes with XPath, even if the original XML document binds the default namespace prefix. Unprefixed names in XPath always seem to be in the 'null' namespace.
I've not really looked into how you map namespace prefixes to URIs with XPath in .NET much, and there are probably better ways of doing it than what I've cobbled together after a quick Googling and reading of MSDN.
EDIT: the aim of my answer was to explain why your code using XPath didn't work. You didn't understand why you were getting no results out of the iterator. My suspicion was that you hadn't given me the full XML document, and that in the part of the document that you didn't share with us laid the answer.
Ultimately, I believe your original code didn't work because of XML namespaces. As I write this edit, I can only get a 'Request has expired' error from the URLs in your comment thread with L.B, so I can no longer test with the same kind of data you're using. However, this error request does begin as follows:
<?xml version="1.0"?>
<ItemLookupErrorResponse xmlns="http://ecs.amazonaws.com/doc/2011-08-01/">
The xmlns attribute puts the element, and every element contained within it, into a namespace. Each namespace is identified by a URI, and together, the URI and the element name identify that element.
It could be that a successful request may have the same attribute. However, L.B.'s answer uses a different namespace, so I can't be sure. For the rest of this edit, I'll have to assume a successful request does contain the same namespace as an unsuccessful one.
Because of this namespace, the element <ItemAttributes> within this XML
<ItemLookupResponse xmlns="http://ecs.amazonaws.com/doc/2011-08-01/">
<ItemAttributes />
</ItemLookupResponse>
and within this XML
<ItemAttributes />
are not the same. The first is in the http://ecs.amazonaws.com/doc/2011-08-01/ namespace, whereas the second is in the namespace identified by the empty string. This empty namespace is the default namespace if it hasn't been set any other way.
Because the two ItemAttributes elements have different namespaces, they're not the same.
As well as changing the namespace of elements by using xmlns="...", you can also associate (or bind) a prefix to a namespace. This is done by specifying the prefix you want to associate with the namespace in an xmlns attribute, using an attribute such as xmlns:prefix="some-uri". This prefix is then put in the XML element before the local name, for example <prefix:SomeElement ... />. This puts the SomeElement element in the namespace associated with the URI some-uri.
Because elements are identified by local name and namespace URI, the following two XML fragments are equal, even though one uses a prefix and the other one doesn't:
<ItemLookupResponse xmlns="http://ecs.amazonaws.com/doc/2011-08-01/">
<ItemAttributes />
</ItemLookupResponse>
<ecs:ItemLookupResponse xmlns:ecs="http://ecs.amazonaws.com/doc/2011-08-01/">
<ecs:ItemAttributes />
</ecs:ItemLookupResponse>
Now we turn to XPath and namespaces. Your XPath expression is
//ItemAttributes[contains(ProductGroup, 'Book')]
One irritation with XPath is that you can't change the namespace used without prefixes in the same way as you can with XML. So the names ItemAttributes and ProductGroup in the above are always in the 'empty' namespace. This XPath expression matches nothing in your XML document because there are no elements with local name ItemAttributes in the 'empty' namespace, let alone any with a ProductGroup child element containing the text Book.
However, with most (if not all) XPath APIs, there are ways of binding prefixes to namespaces. What I did was to show one way of doing this with XPath in .NET. I associated the prefix pfx (I could have chosen any prefix I wanted) with the URI I had used in my example above. You'd use a different URI to my made-up example. You could then use the XPath expression
//pfx:ItemAttributes[contains(pfx:ProductGroup, 'Book')]
to find the relevant element, because there are element(s) with name ItemAttributes and namespace http://ecs.amazonaws.com/doc/2011-08-01/, and at least one of those contains a child element with name ProductGroup in the same namespace and with text contents Book.
Related
As question states. I have a xml document (below) and I need to get X_ScalarWebApi_DeviceInfo that defines namespace urn:schemas-sony-com:av. Unfortunately it results in an error: {System.NullReferenceException: Object reference not set to an instance of an object. I'm mainly interested in ServiceList element, but it doesn't work as well. Platform - Windows 10 mobile.
Any ideas?
<?xml version="1.0"?>
<root xmlns="urn:schemas-upnp-org:device-1-0">
<specVersion>
<major>1</major>
<minor>0</minor>
</specVersion>
<device>
<deviceType>urn:schemas-upnp-org:device:Basic:1</deviceType>
<friendlyName>ILCE-6000</friendlyName>
<manufacturer>Sony Corporation</manufacturer>
<manufacturerURL>http://www.sony.net/</manufacturerURL>
<modelDescription>SonyDigitalMediaServer</modelDescription>
<modelName>SonyImagingDevice</modelName>
<UDN>uuid:000000001000-1010-8000-62F1894EE7BE</UDN>
<serviceList>
<service>
<serviceType>urn:schemas-sony-com:service:ScalarWebAPI:1</serviceType>
<serviceId>urn:schemas-sony-com:serviceId:ScalarWebAPI</serviceId>
<SCPDURL/>
<controlURL/>
<eventSubURL/>
</service>
</serviceList>
<av:X_ScalarWebAPI_DeviceInfo xmlns:av="urn:schemas-sony-com:av">
<av:X_ScalarWebAPI_Version>1.0</av:X_ScalarWebAPI_Version>
<av:X_ScalarWebAPI_ServiceList>
<av:X_ScalarWebAPI_Service>
<av:X_ScalarWebAPI_ServiceType>guide</av:X_ScalarWebAPI_ServiceType>
<av:X_ScalarWebAPI_ActionList_URL>http://192.168.122.1:8080/sony</av:X_ScalarWebAPI_ActionList_URL>
<av:X_ScalarWebAPI_AccessType/>
</av:X_ScalarWebAPI_Service>
<av:X_ScalarWebAPI_Service>
<av:X_ScalarWebAPI_ServiceType>accessControl</av:X_ScalarWebAPI_ServiceType>
<av:X_ScalarWebAPI_ActionList_URL>http://192.168.122.1:8080/sony</av:X_ScalarWebAPI_ActionList_URL>
<av:X_ScalarWebAPI_AccessType/>
</av:X_ScalarWebAPI_Service>
<av:X_ScalarWebAPI_Service>
<av:X_ScalarWebAPI_ServiceType>camera</av:X_ScalarWebAPI_ServiceType>
<av:X_ScalarWebAPI_ActionList_URL>http://192.168.122.1:8080/sony</av:X_ScalarWebAPI_ActionList_URL>
<av:X_ScalarWebAPI_AccessType/>
</av:X_ScalarWebAPI_Service>
</av:X_ScalarWebAPI_ServiceList>
</av:X_ScalarWebAPI_DeviceInfo>
</device>
</root>
Ah, the code:
XDocument xDoc = XDocument.Parse(xml_text);
//var av = xDoc.Root.GetDefaultNamespace();//.Attribute("xmlns");//
XNamespace av = "urn:schemas-sony-com:av";
System.Diagnostics.Debug.WriteLine(av);
<XElement> api_list = (List<XElement>)xDoc.Element(av + "X_ScalarWebAPI_DeviceInfo").Elements();
==EDIT==
Well, both solutions were ok, so I'm upvoting one and marking as answer the other :P
It was mentioned that the solution using only a 'local name' might cause false positive search results, so to be safe I'm using the first one. Thanks for help!
Element only returns elements directly beneath the current node. You need the to specify the entire path (note there are multiple namespaces):
XNamespace ns = "urn:schemas-upnp-org:device-1-0";
XNamespace av = "urn:schemas-sony-com:av";
var api_list = xDoc.Root.Element(ns + "device")
.Element(av + "X_ScalarWebAPI_DeviceInfo").Elements();
Using Xml Linq
XDocument doc = XDocument.Load(FILENAME);
XElement x_ScalarWebAPI_DeviceInfo = doc.Descendants().Where(x => x.Name.LocalName == "X_ScalarWebAPI_DeviceInfo").FirstOrDefault();
XNamespace ns = x_ScalarWebAPI_DeviceInfo.Name.Namespace;
The issue is indeed the Element or Elements methods only search the direct child elements. You can use Decendants() but you will get a collection, so you have to do First(expression) to het a single one.
But in the expression you can use .Name.LocalName to skip the whole namespace thing and look for just the name of the element.
For this question:
XDocument xDoc = XDocument.Parse(xml_text);
XElement x_ScalarWebAPI_DeviceInfo = doc.Descendants().First(x.Name.LocalName == "X_ScalarWebAPI_DeviceInfo");
I have code to get the nodes of a root element:
xmlNodes = rootElement.SelectNodes("DefinitionName");
It's not returning nodes that exist. In the debugger, I can expand rootElement to find DefinitionName. Apparently the problem is the fact that the file has a namespace defined (see XML below). MSDN says that I have to do something like this to get nodes to return:
Note: This has nothing to do with my code. This is the example from MSDN:
XmlNamespaceManager nsmgr = new XmlNamespaceManager(doc.NameTable);
nsmgr.AddNamespace("ab", "http://www.lucernepublishing.com");
XmlNodeList nodelist = doc.SelectNodes("//ab:book", nsmgr);
I have two questions:
Why does the namespace matter? If I want a node, and it exists, just give it to me.
My app processes many XML files. How am I supposed to specify the namespace (nsmgr.AddNamespace())? Do I need to parse the file to get that first?
I can't help but feeling that I'm taking the long, angst-filled way of doing this.
This is the XML:
<?xml version="1.0" encoding="utf-8"?>
<SessionStateInfo xmlns:i="http://www.w3.org/2001/XMLSchema-instance"
z:Id="1" z:Type="Company.Apps.MoreHere.Session.SessionStateInfo"
z:Assembly="assembly info here"
xmlns:z="http://schemas.microsoft.com/2003/10/Serialization/"
xmlns="http://schemas.datacontract.org/2004/07/MoreHere.Session">
<CoaterNumber>25</CoaterNumber>
<DefinitionName z:Id="2">Two Line</DefinitionName>
<EnableManualMode>true</EnableManualMode>
<SessionStateInfo ....
xmlns="http://schemas.datacontract.org/2004/07/MoreHere.Session">
means that this element and all its descendants are in the http://schemas.datacontract.org/2004/07/MoreHere.Session namespace. Since unprefixed names in an XPath always refer to elements in no namespace, you will need to bind this URI to a prefix and use that prefix in your XPath, even though no prefix is in use in the document.
XmlNamespaceManager nsmgr = new XmlNamespaceManager(doc.NameTable);
nsmgr.AddNamespace("mhs", "http://schemas.datacontract.org/2004/07/MoreHere.Session");
xmlNodes = rootElement.SelectNodes("mhs:DefinitionName", nsmgr);
If you know that the element(s) you are looking for will always have the same local name but may or may not have a namespace (or may have different namespaces) then you can use XPath tricks like
rootElement.SelectNodes("*[local-name() = 'DefinitionName']");
It matters because if there's a namespace attached, then "DefinitionName" is not enough. Imagine you have been given a list of people, all with the first name John:
John Smith
John Jones
John Murphy
What you're doing is the equivalent of asking for "John," instead of "John Smith" for example.
This isn't exactly the answer to the question but it may be an alternative solution using XDocument
using System;
using System.Dynamic;
using System.Xml.Linq;
using Microsoft.CSharp.RuntimeBinder;
using System.Linq;
namespace ConsoleApplication8
{
class Program
{
static void Main(string[] args)
{
XDocument document = XDocument.Load("SessionStateInfo.xml");
XNamespace nameSpace = document.Root.GetDefaultNamespace();
XElement node = document.Descendants(nameSpace + "DefinitionName").FirstOrDefault();
if (node != null)
{
Console.WriteLine("Cool! XDocument rocks! value: {0}", node.Value);
}
else
{
Console.WriteLine("Spoot! Didn't find it!");
}
}
}
}
This seems to work if the default namespace is specified or not.
How to parse the xml file?
<?xml version="1.0" encoding="UTF-8"?>
<sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
<sitemap>
<loc>link</loc>
<lastmod>2011-08-17T08:23:17+00:00</lastmod>
</sitemap>
<sitemap>
<loc>link</loc>
<lastmod>2011-08-18T08:23:17+00:00</lastmod>
</sitemap>
</sitemapindex>
I am new to XML, I tried this, but it seems to be not working :
XmlDocument xml = new XmlDocument(); //* create an xml document object.
xml.Load("sitemap.xml");
XmlNodeList xnList = xml.SelectNodes("/sitemapindex/sitemap");
foreach (XmlNode xn in xnList)
{
String loc= xn["loc"].InnerText;
String lastmod= xn["lastmod"].InnerText;
}
The problem is that the sitemapindex element defines a default namespace. You need to specify the namespace when you select the nodes, otherwise it will not find them. For instance:
XmlDocument xml = new XmlDocument();
xml.Load("sitemap.xml");
XmlNamespaceManager manager = new XmlNamespaceManager(xml.NameTable);
manager.AddNamespace("s", "http://www.sitemaps.org/schemas/sitemap/0.9");
XmlNodeList xnList = xml.SelectNodes("/s:sitemapindex/s:sitemap", manager);
Normally speaking, when using the XmlNameSpaceManager, you could leave the prefix as an empty string to specify that you want that namespace to be the default namespace. So you would think you'd be able to do something like this:
// WON'T WORK
XmlDocument xml = new XmlDocument();
xml.Load("sitemap.xml");
XmlNamespaceManager manager = new XmlNamespaceManager(xml.NameTable);
manager.AddNamespace("", "http://www.sitemaps.org/schemas/sitemap/0.9"); //Empty prefix
XmlNodeList xnList = xml.SelectNodes("/sitemapindex/sitemap", manager); //No prefixes in XPath
However, if you try that code, you'll find that it won't find any matching nodes. The reason for this is that in XPath 1.0 (which is what XmlDocument implements), when no namespace is provided, it always uses the null namespace, not the default namespace. So, it doesn't matter if you specify a default namespace in the XmlNamespaceManager, it's not going to be used by XPath, anyway. To quote the relevant paragraph from the Official XPath Specification:
A QName in the node test is expanded into an expanded-name using the
namespace declarations from the expression context. This is the same
way expansion is done for element type names in start and end-tags
except that the default namespace declared with xmlns is not used: if
the QName does not have a prefix, then the namespace URI is null (this
is the same way attribute names are expanded). It is an error if the
QName has a prefix for which there is no namespace declaration in the
expression context.
Therefore, when the elements you are reading belong to a namespace, you can't avoid putting the namespace prefix in your XPath statements. However, if you don't want to bother putting the namespace URI in your code, you can just use the XmlDocument object to return the URI of the root element, which in this case, is what you want. For instance:
XmlDocument xml = new XmlDocument();
xml.Load("sitemap.xml");
XmlNamespaceManager manager = new XmlNamespaceManager(xml.NameTable);
manager.AddNamespace("s", xml.DocumentElement.NamespaceURI); //Using xml's properties instead of hard-coded URI
XmlNodeList xnList = xml.SelectNodes("/s:sitemapindex/s:sitemap", manager);
Sitemap has 2 sub nodes "loc" and "lastmod". The nodes that you are accessing are "name" and "url". that is why you are not getting any result. Also in your XML file the last sitemap tag is not closed properly with a corresponding Kindly try xn["loc"].InnerText and see if you get the desired result.
I would definitely use LINQ to XML instead of the older XmlDocument based XML API. You can accomplish what you are looking to do using the following code. Notice, I changed the name of the element that I am trying to get the value of to 'loc' and 'lastmod', because this is what is in your sample XML ('name' and 'url' did not exist):
XElement element = XElement.Parse(XMLFILE);
IEnumerable<XElement> list = element.Elements("sitemap");
foreach (XElement e in list)
{
String LOC= e.Element("loc").Value;
String LASTMOD = e.Element("lastmod").Value;
}
I am trying to find a node by the name attribute value.
Here is a sample of the xml document:
<?xml version="1.0" encoding="utf-8" standalone="no"?>
<!DOCTYPE kfx:XMLRELEASE SYSTEM "K000A004.dtd">
<kfx:XMLRELEASE xmlns:kfx="http://www.kofax.com/dtd/">
<kfx:KOFAXXML>
<kfx:BATCHCLASS ID="00000008" NAME="CertficateOfLiability">
<kfx:DOCUMENTS>
<kfx:DOCUMENT DOCID="00000006" DOCUMENTCLASSNAME="COL">
<kfx:DOCUMENTDATA>
<kfx:DOCUMENTFIELD NAME="Producer Name" VALUE="Howalt+McDowell Insurance" />
...
....
Here is my attempted expression:
var xml = XDocument.Load(new StreamReader("C:\\Users\\Matthew_cox\\Documents\\test.xml"));
XNamespace ns = "http://www.kofax.com/dtd/";
XmlNamespaceManager nsm = new XmlNamespaceManager(xml.CreateNavigator().NameTable);
nsm.AddNamespace("kfx", ns.NamespaceName);
var docs = xml.Descendants(ns + "DOCUMENT");
foreach(var doc in docs)
{
doc.XPathSelectElement("/DOCUMENTDATA/DOCUMENTFIELD/[#name='Producer Name']", nsm); //this line produces this exception: Expression must evaluate to a node-set.
}
XML is case-sensitive. In provided XML kfx:DOCUMENTFIELD has NAME attribute. Also your XPath doesn't have reference to namespace.
Try this XPath:
kfx:DOCUMENTDATA/kfx:DOCUMENTFIELD[#NAME = 'Producer Name']
I see two things wrong.
First of all you are selecting starting with "/", this selects from the document root, so strip the leading slash.
Secondly the expression is a bit wierd. I would include the condition directly on DOCUMENTFIELD. (I am unsure if no expression on the node axis actually means something. As in is .../[..] equivalent to .../node()[..] or perhaps even .../*[..]?)
As Kirill notes, you should also watch the casing and namespaces, but this should solve c# complaining about expressions not evaluating to node sets:
kfx:DOCUMENTDATA/kfx:DOCUMENTFIELD[#NAME = 'Producer Name']
I have a following XML (part of a .rdl report):
<Report xmlns:rd="http://schemas.microsoft.com/SQLServer/reporting/reportdesigner" xmlns="http://schemas.microsoft.com/sqlserver/reporting/2008/01/reportdefinition">
<DataSources>
<DataSource Name="TMSSharepointDataSource">
<DataSourceReference>TMSSharepointDataSource</DataSourceReference>
<rd:DataSourceID>f06ffa33-238f-4d83-adfe-1eaa8df96e90</rd:DataSourceID>
</DataSource>
</DataSources>
</Report>
I try to parse and read it using following code:
byte[] fileContent = File.ReadAllBytes(#"path");
UTF8Encoding unicode = new UTF8Encoding();
string stringContent = unicode.GetString(fileContent);
XDocument xml = XDocument.Parse(stringContent);
XElement dsNode = xml.Root.Element("DataSources");
I can't figure out why is dsNode always null?
It's a namespace issue... you need to specify the namespace for the DataSources element. Fortunately, LINQ to XML makes this really easy:
XNamespace ns = "http://schemas.microsoft.com/sqlserver/" +
"reporting/2008/01/reportdefinition";
XElement dsNode = xml.Root.Element(ns + "DataSources");
Note the xmlns="http://..." part of the root element, which means that element and all elements below it which don't have an explicit namespace inherit that namespace.
You are probably missing a namespace reference. Your DataSources will inherit the namespace of the Report node and you will need both the namespace and element local name to generate an XName.
Alternatively you can do the following and skip the namespace check:
XElement dsNode =
xml
.Root
.DescendantNodes()
.Where(e => e.Name.LocalName.Equals("DataSources"))
.First();
This will return the first node where the local name is DataSources. In your example this will be the DataSources element.
Also, your loading of the document is very clumsy. I would suggest the following instead:
XDocument xml = XDocument.Load(File.OpenRead(#"path"));