var count = (from p in xmlDoc.Descendants("env:SE04Overprinting")
select p).Count();
throws an exception (kind of, it crashes out when getting there, but using a watch shows this error)
xmlDoc.Descendants("env:SE04Overprinting")
'"env:SE04Overprinting"' threw an exception of type 'System.Xml.XmlException'
System.Collections.Generic.IEnumerable<System.Xml.Linq.XElement>
I did a bit of looking around on this error but didn't find anything, then tried getting rid of the colon:
var count = (from p in xmlDoc.Descendants("env")
select p).Count();
this works. Is stripping the colons out of the file the only way around this?
If your XML properly declares the namespace you can do this:
XNamespace env = "http://namespaceinXml";
var count = (from p in xmlDoc.Descendants(env + "SE04Overprinting")...
Example:
<foo xmlns:env="http://foo.bar">
<env:bar>test</env:bar>
</foo>
Since bar is within the env namespace you can retrieve its value like this:
XDocument xdoc = XDocument.Load("test.xml");
XNamespace env = "http://foo.bar";
string bar = xdoc.Descendants(env+"bar").First().Value; //returns test
Make sure the XNamespace declaration matches exactly the value of the namespace within your XML.
You appear to be thinking of that colon as part of the name of the element(s) you are trying to retrieve. It is not, it is the delimiter between the namespace prefix and the local-name.
Without a namespace declaration, env: is meaningless. You need to prefix the local-name ("SE04Overprinting", in your case) with the actual namespace (look for xmlns:env=, somewhere above the node(s) you are looking for).
See the section: Creating an XName in a Namespace, in the XName documentation
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With