Search code examples
c#.netxml-parsingxml-namespacesxmlexception

XmlDocument from XML string that contains custom namespaces causes XmlException?


I need to create an XmlDocument partly by using old XML and partly by creating new. The problem is that the old XML contains custom namespaces and I can't seem to be able to use them as I get an XmlException. I've tried to add the namespace to many different places but I can't get over the Exception!

The Exception

System.Xml.XmlException was unhandled by user code
    Message='my' is an undeclared prefix. Line 1, position 42.
    Source=System.Xml

My Code

XmlDocument doc = new XmlDocument();
XmlSchema schema = new XmlSchema();
schema.Namespaces.Add("my", "http://foobar.com/");
doc.Schemas.Add(schema);
XmlDeclaration dec = doc.CreateXmlDeclaration("1.0", "UTF-8", null);
doc.AppendChild(dec);

XmlElement root = doc.CreateElement("root");
root.SetAttribute("xmlns:my", "http://foobar.com/");
doc.AppendChild(root);

foreach (var item in GetItems())
{
    XmlElement elem = doc.CreateElement("item");
    elem.SetAttribute("id", item.id);

    // Append body to elem
    XmlElement body = doc.CreateElement("body");
    body.InnerXml = item.Body; // Here is where I get the exception

    elem.AppendChild(body);

    // Append elem to root
    root.AppendChild(elem);
}

Input from Item.Body is similar to

<aaa><bbb my:attr="55">Foo</bbb></aaa>

I expected the output to be similar to

<?xml version="1.0" encoding="utf-8"?>
<root my:attr="http://foobar.com/">
  <item id="12345">
    <body>
        <aaa>
            <bbb my:attr="55">Foo</bbb>
        </aaa>
    </body>
  </item>
</root>

I'm open to alternatives to using this method. After I create the XmlDocument I prettyprint it, validate it against a schema and then push it out for the user to see.


Solution

  • The following is a workaround, best I can come up with:

     XNamespace  my = "http://foobar.com/";
    
     var doc = new XDocument(new XElement("root", 
                    new XAttribute(XNamespace.Xmlns +  "my", my)));
    
     var body = new XElement("body");
     doc.Root.Add(new XElement("item", new XAttribute("id", 12345), body));
    
     string innerItem = @"<aaa><bbb my:attr=""55"">Foo</bbb></aaa>";       
     string itemWrap = @"<wrap xmlns:my=""http://foobar.com/"">" + innerItem + "</wrap>";
    
     XElement item = XElement.Parse(itemWrap);
     body.Add(item.Element("aaa"));
    
     Console.WriteLine(doc);