0

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?

terbubbs
  • 1,512
  • 2
  • 25
  • 48
  • 1
    The easiest way might be to not use `XmlDocument` to create this but rather XmlTextWriter, which will allow you to use namespaces and prefixes: https://learn.microsoft.com/en-us/dotnet/api/system.xml.xmltextwriter?view=netframework-4.8 – LocEngineer Jan 07 '20 at 16:03
  • @LocEngineer I feel like I looked into this before and found that I couldn't create nested elements. I can't seem to find any tutorials that use XmlWriter and also add nested XML elements. – terbubbs Jan 07 '20 at 16:10
  • 2
    Bear in mind that prefixes only have a *local* meaning. You don't need to add an `ns0` prefix, you need to ensure that your elements are associated with the correct *namespace* which will be declared within *that* XML structure. – Damien_The_Unbeliever Jan 07 '20 at 16:11
  • @Damien_The_Unbeliever That's good to know. Thanks – terbubbs Jan 07 '20 at 16:14
  • 1
    @terbubbs Nested elements should not be a problem. If you use `WriteElementString` to write a node, it will automatically close this node so you cannot nest anything there. But if you just use `WriteStartElement` followed by `WriteString` for the content, the element will be open for nesting until you write the corresponding `WriteEndElement`. I find it rather handy in some instances and would definitely use it in your case. – LocEngineer Jan 07 '20 at 16:15
  • @LocEngineer Interesting. I will look into this option then. Thank you! – terbubbs Jan 07 '20 at 16:18

0 Answers0