I am creating a somewhat complex XML file and I need to include the "ns0" prefix to each XmlElement.
Here are the opening lines of code:
var asnFile = new XmlDocument();
var dec = asnFile.CreateXmlDeclaration("1.0", "UTF-8",null);
asnFile.AppendChild(dec);
var advancedShippingNoticesNode = asnFile.CreateElement("AdvancedShippingNotices");
var advancedShippingNoticesNodeAttr = asnFile.CreateAttribute("xmlns");
advancedShippingNoticesNodeAttr.Value = "http://www.testschema.com/schema/AdvancedShippingNotices.xsd";
advancedShippingNoticesNode.Attributes.Append(advancedShippingNoticesNodeAttr);
asnFile.AppendChild(advancedShippingNoticesNode);
var asnIdNode = asnFile.CreateElement("ASNID");
asnIdNode.InnerText = "TestASN";
advancedShippingNoticesNode.AppendChild(asnIdNode);
I have tried adding a prefix in the following way but the prefix never shows up when opening the saved XML file.
advancedShippingNoticesNode.Prefix = "ns0";
I read here that I'm not able to add a prefix, but since I am creating the XmlDocument on the fly and not loading it from an existing file, I feel like this doesn't apply to my case.
I did try the sample solution in the question/answer linked above, but this XmlDocument has so much nesting that it's hard for me to translate that solution into a working solution for myself. I also feel like that is far too complex just to add a prefix.
Is there a simple way to add a prefix to a new XmlDocument?