8

Current File Format

<Folio>
<Node1>Value1</Node1>
<Node2>Value2</Node2>
<Node3>Value3</Node3>
</Folio>

Desired Output

<vs:Folio>
<vs:Node1>Value1</vs:Node1>
<vs:Node2>Value2</vs:Node2>
<vs:Node3>Value3</vs:Node3>
</vs:Folio>

I am using XmlElement and XmlDocument to add the prefix to the child Node element and I'm unable to accomplish it. I would be really grateful if someone could give me the right push in the right direction.

ThinkingStiff
  • 64,767
  • 30
  • 146
  • 239
Venki
  • 1,419
  • 5
  • 28
  • 38
  • 1
    I think that would mean that the Node nodes were in a different namespace from the folio but there is no default schama or namespace attributes defined so what's the point? What are you actually trying to achieve by adding the prefix? – BenCr Mar 01 '11 at 16:45
  • I'm sorry but it doesn't. Are you getting some sort of error? – BenCr Mar 01 '11 at 17:34
  • @BenCr I got the error fixed. :) Thanks for the help!! – Venki Mar 01 '11 at 19:20

2 Answers2

12

If you are trying to add namespace to the elements after loading the xml document then it is not possible.

From MSDN:

You cannot add, modify, or delete an XML namespace definition in an instance of an XML document after the document has been loaded into the XML Document Object Model (XMLDOM) parser. The XML nodes that are used to represent data in the XML document are created when the document is loaded into the XMLDOM parser. These nodes are permanently bound to their XML namespace attributes when they are created. Therefore, the empty XML namespace declaration (xmlns = "") is appended to the child nodes of these nodes to preserve the default XML namespace attribute of these nodes.

However you can load the input, read each element and write it to another document (or in-memory) which has the namespace set. Below is the code that parses the string xml, creates a new xml element along with namespace prefix and namespace.

            String xmlWithoutNamespace =
                @"<Folio><Node1>Value1</Node1><Node2>Value2</Node2><Node3>Value3</Node3></Folio>";
            String prefix ="vs";
            String testNamespace = "http://www.testnamespace/vs/";
            XmlDocument xmlDocument = new XmlDocument();

            XElement folio = XElement.Parse(xmlWithoutNamespace);
            XmlElement folioNode = xmlDocument.CreateElement(prefix, folio.Name.LocalName, testNamespace);

            var nodes = from node in folio.Elements()
                        select node;

            foreach (XElement item in nodes)
            {
                var node = xmlDocument.CreateElement(prefix, item.Name.ToString(), testNamespace);
                node.InnerText = item.Value;
                folioNode.AppendChild(node);
            }

            xmlDocument.AppendChild(folioNode);

xmlDocument now contains the xml with each node prefixed with vs.

Vijay Sirigiri
  • 4,653
  • 29
  • 31
0
 private static void SetPrefix(string prefix, XmlNode node)
    {
        node.Prefix = prefix;
        foreach (XmlNode n in node.ChildNodes)
        {
            //if (node.ParentNode != null)
            //{
            if (n.Name.Contains("QualifyingProperties"))
            {
                break;
            }
            //}
            SetPrefix(prefix, n);
        }
    }
  • 6
    While this may technically answer the question I don't like the notion of some tossed source code being an appropriate answer. Could you please elaborate *how* and *why* this solves the OPs issue? – Paul Kertscher Oct 13 '17 at 07:35