C# Xml SelectSingleNode returns null [duplicate] - c#

This question already has answers here:
SelectSingleNode returns null when tag contains xmlNamespace
(4 answers)
Closed 5 years ago.
I have this xml and want to extract the first Country out of the xml
<string xmlns="http://www.webserviceX.NET">
<NewDataSet>
<Table>
<Country>Hong Kong</Country>
<City>Cheung Chau</City>
</Table>
<Table>
<Country>Hong Kong</Country>
<City>Hong Kong Inter-National Airport</City>
</Table>
</NewDataSet>
</string>
here's what I did:
value = xml.DocumentElement.SelectSingleNode("string/NewDataSet/Table[1]/Country").InnerText;
This always throw an exception not set to an instance of object as the selectsinglenode always retursn null. Strange thing is I have already tested this xpath using this and it does return me the node I want.
I have googled to find a solution and found this suggesting that I have to add namespace, here's what I did:
var nsmgr = new XmlNamespaceManager(xml.NameTable);
nsmgr.AddNamespace("string", "http://www.webserviceX.NET");
var node = xml.DocumentElement.SelectSingleNode("string/NewDataSet/Table[1]/Country", nsmgr);
Still I have the same exception. Can someone please let me know what I'm doing wrong here? Thanks :)

Just use XmlNamespaceManager
XmlNamespaceManager namespaces = new XmlNamespaceManager(xdoc.NameTable);
namespaces.AddNamespace("sp", "http://www.webserviceX.NET");
var nodes = xdoc.DocumentElement.SelectSingleNode("//sp:NewDataSet/sp:Table[1]/sp:Country", namespaces);

Related

How to remove a node from XmlDocument loaded XML [duplicate]

This question already has answers here:
Removing nodes from an XmlDocument
(6 answers)
Closed 6 years ago.
I have repeated xml fragment like this below in a larger document. How to remove all occurrences of well, total and fee using XmlDocument?
<res>
<pay>
<well/>
<total/>
<fee/>
<tit>
<qr>asdf</qr>
<id>
<num/>
</id>
</tit>
</pay>
</res>
Load the xml into an XDocument (https://msdn.microsoft.com/en-us/library/system.xml.linq.xdocument(v=vs.110).aspx) and then use linq to find and remove the nodes in question.
Then its just a duplicate of XDocument deleting a node
If you really want to use the XMLDocument instead of XDocument class then this is a dup of Removing nodes from an XmlDocument

Set XML as Value of XElement [duplicate]

This question already has answers here:
How to avoid System.Xml.Linq.XElement escaping HTML content?
(4 answers)
Closed 7 years ago.
My method receives a XML string as the input and I need to put this XML string into XML envelope using XElement:
input: <hello>Hello!</hello>
expected result: <envelope><hello>Hello!</hello></envelope>
The problem is that this code:
string xmlHello = "<hello>Hello!</hello>";
XElement xelem = new XElement("envelope", xmlHello);
escapes all <> and so the result is:
<envelope><hello>Hello!</hello></envelope>
Is there any way to disable this behaviour of the XElement constructor to be able to accept XML as the value? The input string can be really huge, so I would like to avoid parsing it.
As mentioned in the comments, this can't be done directly as the API has no way of knowing your text is actually well formed XML unless you pass it something it knows is an XML element.
So what you need to do is parse your XML first:
string xmlHello = "<hello>Hello!</hello>";
var hello = XElement.Parse(xmlHello);
var envelope = new XElement("envelope", hello);
Resulting in:
<envelope>
<hello>Hello!</hello>
</envelope>

XPath to href attribute & WriteLine the URL [duplicate]

This question already has answers here:
Get a value of an attribute by XPath and HtmlAgilityPack
(3 answers)
Closed 9 years ago.
I understand there are many XPath href questions, but none suit my case or I am a beginner of it and don't know what's wrong with my code. Kindly bear with me if this is silly question.
I have this HTML structure:
<td valign="top">08-Jan-14 16:02</td>
<td valign="top"><span style="cursor:help;" title="Regulatory News Service">RNS</span></td>
<td valign="top">Blocklisting Interim Review</td>
<td valign="top">Company Announcement - General</td>
My code is:
HtmlNodeCollection cols5 = rows[i].SelectNodes(".//td[3]/a[#href]");
Stream writer to write the URL:
sw.WriteLine(cols5[j].InnerText);
The result appears to be Blocklisting Interim Review instead of the URL. Can anyone kindly look into it? I've went through XPath guide and search all over but still can't get the exact answer for my case. Any help would be much appreciated!
You cannot select attribute with XPath. Select a element and then get it's href attribute. Following xpath selects from third table cell a element which has href attribute (yes, predicate just specifies that attribute should exist, it does not selects attribute):
var a = doc.DocumentNode.SelectSingleNode(".//td[3]/a[#href]");
var href = a.Attributes["href"].Value;
Returns
share-regulatory-news.asp?shareprice=BARC&ArticleCode=d6rr2uxo&ArticleHeadline=Blocklisting_Interim_Review

How to assign plain XML to C# variable [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Use XML Literals in C#?
I can do this in Visual Basic, How can I do this in C#
Dim xmlTree As XElement = <Employees></Employees>
Try:
XDocument document = XDocument.Parse("<Employees></Employees>")
or
XElement root = new XElement("Employees")
Another way is using XmlDocument class:
XmlDocument document = new XmlDocument();
document.LoadXml("<Employees></Employees>");
but I recommend to use XDocument. It is newer than XmlDocument, it has more clean API and support Linq To Xml.
XDocument document = XDocument.Parse("<Employees></Employees>")

difference between XElement and XDocument [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
What's the difference between XElement and XDocument?
What is the difference between XElement and XDocument
The difference is that an XElement type represents an XML fragment while the XDocument type represents an entire XML document with all associated meta-data.
XDocument has a Declaration, Root while XElement is a single node.
By design, the difference (in usage) is very small.
You sometimes need a full XDocument, if you want access to the processing instructions etc.
But the following is fine (for most purposes):
XElement doc = XElement.Load("file.xml");
While a similar construct with XmlElement would not work.

Categories

Resources