简体   繁体   中英

URI to XML parse

I need to convert the URI addres to XML notation. for example

/Test1/Test2/Test3/

to

<Modul>
<Test1/><Test2/><Test3/>
</Modul>

Here is my code:

private static XmlNode NodeRecurs(XmlNode node, string nodeName)
{
    string[] array = nodeName.Split('/');
    var xdoc = new XmlDocument();
    var name = nodeName.Remove(0, array[0].Length + 1);
    XmlNode xmlNode = xdoc.CreateNode(XmlNodeType.Element, array[0], string.Empty);
    node.AppendChild(xmlNode);

    if (array.Count() != 0)
    {
        NodeRecurs(node, name);
    }

    return node;
}

When the NodeRecurs calls itself it is InvalidArgument exeption. It says the it is wrong context for the current node.

To append nodes to a document, they need to be created by the same document.

You are creating a new XmlDocument every time you call the function - create one outside of the function and pass it in as a parameter.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM