tags:

views:

34

answers:

2

I have the following xml:

<span>sometext</span>

and I want to wrap this XmlNode with another tag:

<p><span>sometext</span></p>

How can i achieve this. For parsing i use XmlDocument (C#).

A: 

You must use the CreateNode(XmlNodeType.Element, "p", "") of XmlDocument.

Then append the old node to the new one with the AppendChild method

Enri
A: 

Hello, you can try something like this.

        string xml = "<span>sometext</span>";
        XmlDocument xDoc = new XmlDocument();
        xDoc.LoadXml(xml);
        // If you have XmlNode already, you can start from this point
        XmlNode node = xDoc.DocumentElement;
        XmlNode parent = node.ParentNode;
        XmlElement xElement = xDoc.CreateElement("p");
        parent.RemoveChild(node);
        xElement.AppendChild(node);
        parent.AppendChild(xElement);
Andrew