0

I have an xml like this :

<name>

<class>

</class>

</name>

then i want to add the tag like this

<tia:Demographic><Age/><DOB/></tia:Demographic>

in between

<class>

How can we do that .

i am using following code.

XDoc.LoadXml(@"<name><class></class></name>");   
XmlDocumentFragment xfrag = XDoc.CreateDocumentFragment(); 
 xfrag.InnerXml = @"<tia:Demographic><Age/><DOB/></tia:Demographic>";
 XDoc.DocumentElement.FirstChild.AppendChild(xfrag); 
XDoc.Save(@"D:\test.xml");

but it throws an error that tia: not a registered namespace

2 Answers 2

4

Register the namespace using a XmlNamespaceManager .

Sign up to request clarification or add additional context in comments.

Comments

2

You will need to register a namespace:

class Program
{
    static void Main()
    {
        var xdoc = new XmlDocument();
        xdoc.LoadXml(@"<name><class></class></name>");
        xdoc.DocumentElement.SetAttribute("xmlns:tia", "http://tia.com");
        var node = xdoc.CreateElement("tia", "Demographic", "http://tia.com");
        var xfrag = xdoc.CreateDocumentFragment();
        xfrag.InnerXml = @"<Age/><DOB/>";
        node.AppendChild(xfrag);
        xdoc.DocumentElement.FirstChild.AppendChild(node);
        xdoc.Save(Console.Out);
    }
}

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.