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>")
Related
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);
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
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>
This question already has answers here:
Writing/outputting HTML strings unescaped
(7 answers)
Closed 7 years ago.
I have an XML file having elements like this
<component name="component1">
<title><![CDATA[Title <span>(Subtitle)</span>]]></title>
</component>
I want to render the title in an HTML page. I can read the title element and when I am rendering it in the HTML page(in browser) I am getting as Title<span>(Subtitle)</span> in the page. In the HTML source it is as Title<span>(Subtitle)</span>
How can I overcome this.
I am using C# for reading the XML data
XmlDocument document = new XmlDocument();
document.Load("pathofdocument");
XmlNodeList nodes = document.DocumentElement.SelectNodes("/list/components/componet");
foreach (XmlNode node in nodes)
{
string title = node.SelectSingleNode("title").InnerText;
}
Why don't you use HttpUtility.HtmlDecode Method
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.