How do I get the innertext of the OverallResult element using SelectSingleNode?
<?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>
<ProcessResp xmlns="http://www.w3.org/2005/Atom">
<Result>
<OverallResult>Success</OverallResult>
var doc = new XmlDocument();
doc.LoadXml(xml);
var text = doc.SelectSingleNode("Example/Node/text()").InnerText; // or .Value
returns
"Some text here\r\n "
and text.Trim() returns
"Some text here"
Please go through the below link for XML related queries its very useful for begginers!
http://www.dotnetcurry.com/ShowArticle.aspx?ID=564
for your answer(i havnt tried it though try by changing like this in ur code)
doc.SelectSingleNode("soap/ProcessResp/ Result/OverallResult/text()").InnerText;
Related
I read multiple feed from many sources with C# Console, and i have this code where i load XML From sources:
XmlDocument doc = new XmlDocument();
doc.Load(sourceURLX);
XElement xdoc = XElement.Load(sourceURLX);
How to get enclosure url and show as variable?
If I understand your question correctly (I'm making a big assumption here) - you want to select an attribute from the root (or 'enclosing') tag, named 'url'?
You can make use of XPath queries here. Consider the following XML:
<?xml version="1.0" encoding="utf-8"?>
<root url='google.com'>
<inner />
</root>
You could use the following code to retrieve 'google.com':
String query = "/root[1]/#url";
XmlDocument doc = new XmlDocument();
doc.Load(sourceURLX);
String value = doc.SelectSingleNode(query).InnerText;
Further information about XPath syntax can be found here.
Edit: As you stated in your comment, you are working with the following XML:
<item>
<description>
</description>
<enclosure url="blablabla.com/img.jpg" />
</item>
Therefore, you can retrieve the url using the following XPath query:
/item[1]/enclosure[1]/#url
With xml like below
<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0">
<channel>
<title>title</title>
<link>https://www.link.com</link>
<description>description</description>
<item>
<title>RSS</title>
<link>https://www.link.com/xml/xml_rss.asp</link>
<description>description</description>
<enclosure url="https://www.link.com/media/test.wmv"
length="10000"
type="video/wmv"/>
</item>
</channel>
</rss>
You will get url by reading attribute
var document = XDocument.Load(sourceURLX);
var url = document.Root
.Element("channel")
.Element("item")
.Element("enclosure")
.Attribute("url")
.Value;
To get multiple urls
var urls = document.Descendants("item")
.Select(item => item.Element("enclosure").Attribute("url").Value)
.ToList();
Using foreach loop
foreach (var item in document.Descendants("item"))
{
var title = item.Element("title").Value;
var link = item.Element("link").Value;
var description = item.Element("description").Value;
var url = item.Element("enclosure").Attribute("url").Value;
// save values to database
}
I'm trying to read the APP_DATE element in this xml message. Tried using LINQ but unable to read MyResult
<?xml version="1.0" encoding="utf-8" ?>
<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<soap:Body>
<MyResponse xmlns="http://tempuri.org/">
<MyResult>
<APP_TYPE>I</APP_TYPE>
<APP_NO>152240</APP_NO>
<APP_DATE>10/03/2016</APP_DATE>
</MyResult>
</MyResponse>
</soap:Body>
</soap:Envelope>
Can't be sure because you didn't supply any example code but I assume you struggle because of the namespaces. So try this:
XmlDocument doc = new XmlDocument();
doc.Load("data.xml");
XmlNamespaceManager ns = new XmlNamespaceManager(doc.NameTable);
ns.AddNamespace("soap", "http://www.w3.org/2003/05/soap-envelope");
ns.AddNamespace("x", "http://tempuri.org/");
var result = doc.SelectSingleNode("//soap:Envelope/soap:Body/x:MyResponse/x:MyResult/x:APP_DATE", ns).InnerText;
For deeper understanding you can read this question
Using Linq it will be like this:
var result = XDocument.Load("data.xml").Root
.Descendants(XName.Get("APP_DATE", "http://tempuri.org/"))
.FirstOrDefault()?.Value;
See how in both examples I have to specify the namespaces of what I'm looking for
I have a webservice and it's response is:
<?xml version="1.0" encoding="utf-8" ?>
<string xmlns="http://tempuri.org/">
<contractor-management>
<request-attributes> <corp-id>abcd</corp-id></request-attributes>
</contractor-management>
</string>
I am getting the above response but I want to remove the string tag line. So it should be like this:
<?xml version="1.0" encoding="utf-8" ?>
<contractor-management>
<request-attributes> <corp-id>abcd</corp-id></request-attributes>
</contractor-management>
Due to some reason("-" cannot be used as a part of variable name), I am creating the above like this:
string x;
x = "<contractor-management>";
x += "<request-attributes> ";
x += "<corp-id>abcd</corp-id> ";
x += "</request-attributes> ";
x = "</contractor-management>";
Do like below code, where xmlContent is your current xml contents
XmlDocument xmldoc = new XmlDocument();
xmldoc.LoadXml(xmlContent);
XmlNode firstChild=xmldoc.GetElementsByTagName("string")[0];
XmlNode secondChild = firstChild.FirstChild;
xmldoc.ReplaceChild(secondChild, firstChild);
You can also save this modified xml to a file
xmldoc.Save(#"modified.xml");
<?xml version="1.0" encoding="UTF-16"?>
<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><GetXmlDocumentResponse xmlns="http://tempuri.org/">
<GetXmlDocumentResult>
<SNR_JO_POST xmlns="">
<Sectors>
<LawyerName> abc</LawyerName>
<ID>{B263A7B1-D766-4308-B486-C63BE66F4D74}</ID>
<Email>abc#gmail.com</Email>
</Sectors>
</SNR_JO_POST>
</GetXmlDocumentResult>
</GetXmlDocumentResponse>
</soap:Body>
</soap:Envelope>
the above is soap response , I want to get values of LawyerName , ID and Email from the response .
How could i use this response in my c# code .
You can use Linq To Xml for this
var xDoc = XDocument.Parse(xmlstring);
var sectors = xDoc.Descendants("Sectors").FirstOrDefault();
var lawyerName = (string)sectors.Element("LawyerName");
var id = (string)sectors.Element("ID");
I have the following SOAP response:
<?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>
<ABRSearchByABNResponse xmlns="http://abr.business.gov.au/ABRXMLSearch/">
<ABRPayloadSearchResults>
<request>
<identifierSearchRequest>
<identifierType>ABN</identifierType>
<identifierValue>79 142 357 604</identifierValue>
</identifierSearchRequest>
</request>
<response>
<dateRegisterLastUpdated>2011-04-26</dateRegisterLastUpdated>
<dateTimeRetrieved>2011-04-26T14:10:17.8169921+10:00</dateTimeRetrieved>
<businessEntity>
<recordLastUpdatedDate>2010-03-05</recordLastUpdatedDate>
<ABN>
<identifierValue>79142357604</identifierValue>
<isCurrentIndicator>Y</isCurrentIndicator>
<replacedIdentifierValue xsi:nil="true" />
<replacedFrom>0001-01-01</replacedFrom>
</ABN>
<entityStatus>
<effectiveTo>0001-01-01</effectiveTo>
</entityStatus>
<entityType>
<entityTypeCode>PUB</entityTypeCode>
</entityType>
<mainBusinessPhysicalAddress>
<stateCode>NSW</stateCode>
<postcode>2000</postcode>
</mainBusinessPhysicalAddress>
</businessEntity>
</response>
</ABRPayloadSearchResults>
</ABRSearchByABNResponse>
</soap:Body>
</soap:Envelope>
What I am trying to get is the entityTypeCode but I have no success. I tried with
XmlDocument xDoc = new XmlDocument();
xDoc.LoadXml(searchPayload);
XmlNamespaceManager nsmgr = new XmlNamespaceManager(xDoc.NameTable);
nsmgr.AddNamespace("c", "http://abr.business.gov.au/ABRXMLSearch/");
XmlNode entityTypeCode = xDoc.SelectSingleNode("//entityTypeCode", nsmgr);
and various xpath expressions but it the xmlnode entityTypeCode is always null.
Suggestions?
Thanks in advance.
use XmlNode entityTypeCode = xDoc.SelectSingleNode("//c:entityTypeCode", nsmgr); as you have added the namespace of the element as c prefix in the namespace manager.