Reputation: 4129
So, I'm loading the XElement
of a document like this:
Root = XElement.Load(Path);
The original header looks like this:
<?xml version="1.0" encoding="iso-8859-1"?>
<!--Some comment -->
When I call Root.Save("file.xml");
it changes the header declaration and erases the comment. Why is this happening? What can I do besides creating a new whole xml with XDocument
to avoid this?
Upvotes: 0
Views: 389
Reputation: 13073
The shortest way is through XDocument
:
XDocument doc = new XDocument(
nex XDeclaration("1.0", "iso-8859-1", "no"),
XElement.Load(Path)
);
doc.Add(new XComment("Some comment"));
If you want to retain the original, regardless of input, you're going to need XDocument.Load
anyway...
Upvotes: 0
Reputation: 101731
use XDocument.Load
instead of XElement.Load
.
XML declarations are belong to XDocument
not XElement
. XElement
is just loads Root
element.See this and this for more details
Upvotes: 2