How to Extract an Element using XPathSelectElement - c#

I am extracting an element from an xml document but it's returning null
<?xml version="1.0" encoding="utf-8"?>
<Test1
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-test"
xmlns="https://www.google.com/partner/testt.xsd">
<Test2>OK</Test2>
<Test3>1439379003</Test3>
</Test1>
I'm trying to extract test2 element but its returning null
var responseXdoc = XDocument.Parse(response);
var statusElement = responseXdoc.XPathSelectElement("/Test1/Test2");
result statusElement as null but I'm expecting Ok
Problem in Namespace
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="https://www.google.com/partner/testt.xsd (Its my guess)

Your XML has default namespace which elements in the scope inherit implicitly. To reference elements in namespace using XPath, you need to use namespace prefix which, you need to register before in an XmlNamespaceManager :
var nsManager = new XmlNamespaceManager(new NameTable());
nsManager.AddNamespace("d", "https://www.insuranceleads.com/partner/PricePresentationResult.xsd");
var statusElement = responseXdoc.XPathSelectElement("/d:Test1/d:Test2", nsManager);
dotnetfiddle demo
Alternatively, you can use XNamespace and LINQ API to do the same, for example :
XNamespace d = "https://www.insuranceleads.com/partner/PricePresentationResult.xsd";
var statusElement = responseXdoc.Element(d + "Test1").Element(d + "Test2");

Related

c# get values from xml response

I am trying to get values from xml respone :
<?xml version="1.0" encoding="utf-8"?>
<Response xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://adaddaasd.com">
<A>14</A>
<B>Failed</B>
<C>22</C>
</Response>
My code is :
string responseString = await response.Content.ReadAsStringAsync();
var xDocument = XDocument.Parse(responseString);
var responseNode = xDocument.XPathSelectElement("/Response");
var A = xDocument.XPathSelectElement("/Response/A");
But I am getting null values for A and responseNode. Whats wrong? Thanks
You're blatantly ignoring the XML namespace that's defined in your XML document:
<Response xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'
xmlns:xsd='http://www.w3.org/2001/XMLSchema'
xmlns='http://adaddaasd.com'>
****************************
You need to include that into your querying - I would try to do it like this:
var xDocument = XDocument.Parse(responseString);
// *define* your XML namespace!
XNamespace ns = "http://adaddaasd.com";
// get all the <Response> nodes under the root with that XML namespace
var responseNode = xDocument.Descendants(ns + "Response");
// from the first <Response> node - get the descendant <A> nodes
var A = responseNode.FirstOrDefault()?.Descendants(ns + "A");
If you insist on using the XPathSelectElement method, then you must define an XmlNamespaceManager and use it in your XPath select:
// define your XML namespaces
XmlNamespaceManager xmlnsmgr = new XmlNamespaceManager(new NameTable());
xmlnsmgr.AddNamespace("ns", "http://adaddaasd.com");
// use the defined XML namespace prefix in your XPath select
var A = xDocument.XPathSelectElement("/ns:Response/ns:A", xmlnsmgr);

Linq to XML with Namespace Prefix Not Working

I have been working from Scott Allen's Puralsight course on Linq Fundamentals and this linked section describes reading xml with Namespace prefixes, yet I'm not able to make this work.
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<OutboundEvent
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="urn:abb.com:assetsuite:IF:Event http://apifw/devifa/xml/GeneralLedger/GeneralLedger.Publish.Event.V940.xsd"
xmlns="urn:abb.com:assetsuite:IF:Event"
xmlns:hdr="urn:abb.com:assetsuite:IF:Header"
xmlns:bo="urn:abb.com:assetsuite:IF:BO"
xmlns:boe="urn:abb.com:assetsuite:IF:IFExtension"
>
...
<bo:BusinessObject>
...
</bo:BusinessObject>
</OutboundEvent>
I should be getting a single XElement returned from the following code, yet I am not:
var document = XDocument.Load(path);
var bo = (XNamespace)"urn:abb.com:assetsuite:IF:BO";
var businessobjects =
from elements in document.Element("OutboundEvent")?.Elements(bo + "BusinessObject")
?? Enumerable.Empty<XElement>()
select elements;
The OutboundEvent node is in a namespace but your code references it as if it wasn't. It is in the urn:abb.com:assetsuite:IF:Event namespace.
XNamespace ns = "urn:abb.com:assetsuite:IF:Event";
XNamespace bo = "urn:abb.com:assetsuite:IF:BO";
var businessObjects =
document.Elements(ns + "OutboundEvent").Elements(bo + "BusinessObject");
Shouldn't you define and use the default namespace for OutboundEvent element?
var document = XDocument.Load(path);
var ns = (XNamespace)"urn:abb.com:assetsuite:IF:Event";
var bo = (XNamespace)"urn:abb.com:assetsuite:IF:BO";
var businessobjects =
from elements in document.Element(ns + "OutboundEvent")?.Elements(bo + "BusinessObject")
?? Enumerable.Empty<XElement>()
select elements;

Cannot parse XML with xmlns value

I receive an XML file which has on the root node a xmlns namespace assigned:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Message xmlns="http://www.myAddress.com/DataRequest/message/">
<Date>2017/01/01</Date>
</Message>
I do not know how to retrive the Date element using XPath, I tried
var root = xDocument.Root;
var dateElement = root.XPathSelectElement("/Message/Date");
If I remove the namespace from the root xml, then I can retrieve the value using "/Message/Date".
I tried to add xmlns to a XmlNamespaceManager, but I get this error:
Prefix "xmlns" is reserved for use by XML.
How can I get the value?
You should use namespace when you specify element's name. Default namespace is easy to get with XElement.GetDefaultNamespace() method:
var ns = root.GetDefaultNamespace();
var dateElement = (DateTime)root.Element(ns + "Date");
If you want to use XPath:
XmlNamespaceManager manager = new XmlNamespaceManager(new NameTable());
manager.AddNamespace("ns", root.GetDefaultNamespace().ToString());
var dateElement = (DateTime)root.XPathSelectElement("/ns:Message/ns:Date", manager);
I would suggest using LINQ.
Here is a link to the code example:https://msdn.microsoft.com/en-us/library/mt693115.aspx
here is the code:
XElement root = XElement.Load("Message.xml");
IEnumerable<XElement> dateNode=
from el in root.Elements("Date")
select el;
foreach (XElement el in dateNode)
Console.WriteLine(el);

Finding Correct Xpath C# [duplicate]

I'm having this XML document with namespaces and I want to extract some nodes using XPath.
Here's the document:
<ArrayOfAnyType xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://tempuri.org/">
<anyType xsi:type="Document">
<Id>5</Id>
<Title>T1</Title>
</anyType>
<anyType xsi:type="Document">
<Id>15</Id>
<Title>T15</Title>
</anyType>
</ArrayOfAnyType>
What's the XPath expression going to be if I want to extract all "anyType" elements with xsi:type="Document"?
I've tried this:
//anyType[#xsi:type="Document"]
and it doesn't work:
If you are using C# then you need to specify the namespace for the "anyType" element in your XPath:
var xml = new XmlDocument();
xml.LoadXml( "your xml" );
var names = new XmlNamespaceManager( xml.NameTable );
names.AddNamespace( "xsi", "http://www.w3.org/2001/XMLSchema-instance" );
names.AddNamespace( "a", "http://tempuri.org/" );
var nodes = xml.SelectNodes( "//a:anyType[#xsi:type='Document']", names );
I think that
//anyType[namespace-uri() = "http://www.w3.org/2001/XMLSchema-instance"][local-name() = "type"]
Will do what you want.
This way you don't need to specify namespace:
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.LoadXml("your xml");
XmlNode node = xmlDoc.SelectSingleNode("/*[local-name() = 'anyType']");
XmlNode nodeToImport = xmlDoc2.ImportNode(node, true);
xmlDoc2.AppendChild(nodeToImport);
Had nearly the same problem, I forgot to add the correct namespace for xsi:type
(http://www.w3.org/2001/XMLSchema-instance) was using http://www.w3.org/2001/XMLSchema
and I did never get any result - now it is working the following way:
<xsl:value-of select="/item1/item2/item3/#xsi:type"></xsl:value-of>

Cannot get XPath to work with unnamed namespace

The XML (fragment):
<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<soap:Body>
<GetXMLResponse xmlns="http://sitecore.net/visual/">
<GetXMLResult>
<sitecore xmlns="">
<status>ok</status>
The code (fragment):
XmlNamespaceManager nsManager = new XmlNamespaceManager(template.NameTable);
nsManager.AddNamespace("soap", "http://schemas.xmlsoap.org/soap/envelope/");
nsManager.PushScope();
XmlNode sitecoreRoot = template.SelectSingleNode("/soap:Envelope/soap:Body/*[namespace-uri()='http://sitecore.net/visual/' and local-name()='GetXMLResponse']", nsManager);
string status = sitecoreRoot.SelectSingleNode("/GetXMLResult/sitecore/status").Value;
sitecoreRoot element returns the correct node. However the XPath to get the status always returns null, even though the siteCoreRoot.OuterXMl property shows the element is present.
The only thing I can think of is the line:
<sitecore xmlns="">
is throwing off the XPath
TIA
XmlNamespaceManager ns = new XmlNamespaceManager(cd.NameTable);
ns.AddNamespace("sp", "http://schemas.xmlsoap.org/soap/envelope/");
ns.AddNamespace("sc", "http://sitecore.net/visual/");
XmlNode sitecoreRoot = cd.SelectSingleNode("//sp:Envelope/sp:Body/sc:GetXMLResponse/sc:GetXMLResult/sitecore/status", ns);
var status = sitecoreRoot.InnerText;
May help you?
If you just want the status node, you can try this xml library and the following XPath.
XElement root = XElement.Load(file); // or .Parse(string)
XElement status = root.XPathElement("//status");
The library handles doing the namespace for you.

Categories