I need some help parsing this XML.
I recieve the following string and I need to obtain the value of "MensajeError".
<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<WS_SSPBA_001_SResponse xmlns="http://tempuri.org/">
<WS_SSPBA_001_SResult>
<Estado>boolean</Estado>
<Mensaje>string</Mensaje>
<CodigoError>string</CodigoError>
<MensajeError>error1</MensajeError>
</WS_SSPBA_001_SResult>
</WS_SSPBA_001_SResponse>
</soap:Body>
</soap:Envelope>
I made it to the Body tag but I can't manage to parse further down the XML
var xDocument = XDocument.Parse(resultado);
XNamespace soapenv = "http://schemas.xmlsoap.org/soap/envelope/";
var xElements =
xDocument.Descendants(soapenv + "Body").First()
Anything I tried to parse que tag "" failed. I only need to retrieve the tag "MensajeError"
Thanks!
You could just use the LocalName:
var nodeValue = XDocument.Parse(resultado)
.Descendants()
.First(n => n.Name.LocalName == "MensajeError")
.Value;
//nodeValue = "error1"
Related
As I am a Beginner i Certainly need your Help.
I want to get a Specific Tag from Xdocument.
Following is the Content in the Xdocument:
<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>
<UcicLoginResponse xmlns="http://tempuri.org/">
<UcicLoginResult>
<Success>true</Success>
<authToken>xxxxxxx</authToken>
</UcicLoginResult>
</UcicLoginResponse>
</soap:Body>
</soap:Envelope>
Then I want to get the Value of the tag authToken.
Tried Lot with Descentants and Elements..But,due to the xml attributes,All tries leds to error.Anyone pls Help me...
Some of my tries Given:
XDocument _Xresult = XDocument.Parse(XmlResponse.Elements().Single().Value);
IEnumerable<XElement> xResponseItem = _Xresult.Descendants("UcicLoginResult");
if (xResponseItem.Descendants("Remarks").Any())
{
string sErr = _Xresult.Element("Remarks").Value;
throw new Exception("Authentication failed : " + sErr);
}
token = _Xresult.Descendants("authToken").FirstOrDefault().Value;
#
var root = XmlResponse.Root;
var res1= root.Elements("UcicLoginResult").Elements("authToken").FirstOrDefault().Value;
#
var resp=XmlResponse.Descendants("soap:Envelope").Descendants("soap:Body").Descendants("UcicLoginResponse").Descendants("UcicLoginResult").Elements("authToken");
#
IEnumerable<XElement> xResponseItem =XmlResponse.Descendants("UcicLoginResponse");
string sErr = xResponseItem.Descendants("UcicLoginResult").FirstOrDefault().Element("authToken").Value;
#
var res = XmlResponse.Descendants("soap:Envelope").Descendants("soap:Body").Descendants("UcicLoginResponse").Descendants("UcicLoginResult").Elements("authToken");
You are not specifying the namespace. http://tempuri.org/;
var xDocument = XDocument.Parse(xml);
XNamespace ns = "http://tempuri.org/";
var authToken = xDocument.Descendants(ns + "authToken").FirstOrDefault();
I don't know the xml where is it come from but it seems that you are communicating with a SOAP service and it could be better to get the data as object based by WCF client.
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);
I have a XML document that's in the following format:
<?xml version="1.0" encoding="utf-8"?>
<?mso-infoPathSolution name="urn:schemas-microsoft-com:office:infopath:blah:-myXSD-2007-03-14T00-26-45" solutionVersion="1.0" productVersion="15.0.0.0" PIVersion="1.0.0.0" href="http://blah/FormServerTemplates/blah.xsn"?>
<?mso-application progid="InfoPath.Document" versionProgid="InfoPath.Document.4"?>
<my:myFields xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xhtml="http://www.w3.org/1999/xhtml" xmlns:dfs="http://schemas.microsoft.com/office/infopath/2003/dataFormSolution" xmlns:pc="http://schemas.microsoft.com/office/infopath/2007/PartnerControls" xmlns:ma="http://schemas.microsoft.com/office/2009/metadata/properties/metaAttributes" xmlns:d="http://schemas.microsoft.com/office/infopath/2009/WSSList/dataFields" xmlns:q="http://schemas.microsoft.com/office/infopath/2009/WSSList/queryFields" xmlns:dms="http://schemas.microsoft.com/office/2009/documentManagement/types" xmlns:my="http://schemas.microsoft.com/office/infopath/2003/myXSD/2007-03-14T00:26:45" xmlns:xd="http://schemas.microsoft.com/office/infopath/2003" xml:lang="en-US">
<my:workDay>MON-FRI</my:workDay>
</my:myFields>
I tried to query the workday with the following code but got null.
var xdoc = XDocument.Load(file);
var workDay = from x in xdoc.Descendants("workDay")
select x.Value;
What did I do wrong?
you need to include the namespace for my::
var xdoc = XDocument.Load(file);
XNamespace myNamespace = "http://schemas.microsoft.com/office/infopath/2003/myXSD/2007-03-14T00:26:45";
var workDay = from x in xdoc.Descendants(myNamespace + "workDay")
select x.Value;
I am to get the element value of Soap based retuned XML as below.
XML File:
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<SendToDSSResponse xmlns="http://tempuri.org/">
<SendToDSSResult>
<RC>0</RC>
<RCD></RCD>
<PCKT>
<IDNO>1212</IDNO>
<IDTYPE>051</IDTYPE>
<NOBOX>121216</NOBOX>
<NAME>James</NAME>
</PCKT>
</SendToDSSResult>
</SendToDSSResponse>
</soap:Body>
</soap:Envelope>
Now I want to get the values of IDNO, NoBox and Name. I am trying to use the following code below to get the values but it throws an Exception. What's the correct way to get the element values?
C# Code:
var xDoc = XDocument.Parse(cleanXml); //OR XDocument.Load(filename)
string Name = xDoc.Descendants("Name").First().Value;
I think you should add XNamespace and then you can read out the specific value from the nodes or tags under the node, try this demo in your ConsoleApplication:
XDocument doc = XDocument.Load("XMLFile1.xml");
var result = doc.Descendants(XNamespace.Get("http://tempuri.org/")+"NAME").First();
Console.WriteLine(result.Value);
Use Root property.
string name = xDoc.Root.Descendants("NAME").First().Value;
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.