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.
Related
This question already has answers here:
XSLT with XML source that has a default namespace set to xmlns
(3 answers)
Closed 2 years ago.
I have an XML and I wanna get a specific nested element using XPath but it does not work. Maybe that is why tags include xmlns attributes. Any suggestions?
<DataPDU xmlns="urn:cma:stp:xsd:stp.1.0">
<Body>
<AppHdr xmlns="urn:iso:std:iso:20022:tech:xsd:head.001.001.01">
<test1>
<test2>
<test3>
<test4>text</test4>
</test3>
</test2>
</test1>
<test5>
<test6>
<test7>
<test8>dummy text</test8>
</test7>
</test6>
</test5>
<test9>test</test9>
<test10>p1212121</test10>
<test11>147</test11>
<test12>lorem</test12>
</AppHdr>
<Document xmlns="urn:iso:std:iso:20022:tech:xsd:pacs.008.001.08">
<test13>
<test14>
<test15>asap</test15>
</test14>
</test13>
</Document>
</Body>
Yes you'll have to define the namespace. Have a look at this -
How to get XML element with namespace
Or you can pass the xml as string and use regex to remove those attributes -
Regex.Replace(xmlString, #"(\sxmlns="".+"")", "")
Depending on your use case, you can use either.
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 an answer here:
XSLT transforming name value pairs to its corresponding XML
(1 answer)
Closed 8 years ago.
I would like to transform my name/value-pairs to XML via XSLT Transformation.
This should work with the XslCompiledTranform. All this seems pretty clear. But what is the best way to use the name/value pairs? Using an XML to transform to HTML and things like that are pretty clear. I am just confused about the unstructured name/value pairs.
As Tim C says in the comments for the question, XSLT is used to transform XML documents into other documents, usually other XML documents. It can't be used to transform .NET collections, unless you first serialize them into an XML document, in which case you don't need XSLT to turn a collection into XML.
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>")