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.

3
  • 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 1, 2011 at 16:45
  • I'm sorry but it doesn't. Are you getting some sort of error?
    – BenCr
    Mar 1, 2011 at 17:34
  • @BenCr I got the error fixed. :) Thanks for the help!!
    – Venki
    Mar 1, 2011 at 19:20

2 Answers 2

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.

0
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);
        }
    }
1
  • 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? Oct 13, 2017 at 7:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.