I have the following input
<row test="1" />
and want to generate the following output when using XmlTextWriter.
<?xml version="1.0"?>
<root xmlns="urn:default">
<row test="1" />
</root>
According to the documentation for InnerXml ( MSDN ), the following code should work correctly.
var outputdoc = new XmlDocument();
outputdoc.AppendChild(outputdoc.CreateXmlDeclaration("1.0", string.Empty, string.Empty));
outputdoc.AppendChild(outputdoc.CreateElement("root", "urn:default"));
outputdoc.DocumentElement.InnerXml = "<row test=\"1\" />";
var writer = new XmlTextWriter(filename, Encoding.UTF8) { Formatting = Formatting.Indented, Indentation = 1 };
outputdoc.WriteTo(writer);
writer.Close();
Instead, I get the following output:
<?xml version="1.0"?>
<root xmlns="urn:default">
<row test="1" xmlns="" />
</root>
What do I need to do?
EDIT:
I did not make the possible input clear enough. It was supposed to be a piece of Xml, so it could be one element, more than one element with any number of children anyway. For instance:
<row test="1" />
or
<row test="1" />
<row test="2" />
or
<row><test>1</test></row>
or
<row><test>1</test></row>
<row test="2" />
source
share