I was trying to append the xml file in the existing file, everything works fine but I have an issue with default namespace when it appends.
This is the code I use to append:
XmlNode newChild = doc.CreateNode(XmlNodeType.Element, "image", "");
newChild.Attributes.Append(doc.CreateAttribute("name", filename));
XmlNode xmlElement = doc.CreateNode(XmlNodeType.Element, "width", null);
xmlElement.InnerText = widthValue[1].TrimStart();
newChild.AppendChild(xmlElement);
am getting an output like below
<image d2p1:name="" xmlns:d2p1="test.jpg">
<width>1024</width>
</image>
but I was trying to append like:
<image name="test.jpg">
<width>1024</width>
</image>
As others suggested, using LINQ to XML might be easier in general.
But if you want to stick with using
XmlDocument, to fix the issue, change your code to the following:The problem with the code you have is that
doc.CreateAttribute("foo", "bar")creates an attribute with the namefooin a namespace with the URIbar. That’s really not what you want.