NurSoNTyp
NurSoNTyp

Reputation: 11

Create xml structure from string.split

I want to create a xml file from a string that looks like this:

"Node_1/Node_2/Node_3|Node_1/Node_4|Node_1/Node_2/Node_5"

the output should be:

<Node_1>
    <Node_2>
        <Node_3>
        </Node_3>
        <Node_5>
        </Node_5>
    </Node_2>
    <Node_4>
    </Node_4>
</Node_1>

the string should resemble something like a file path and the nodes in the xml should resemble something like folders. The first "Folder" is allways the same (Node_1) to keep it a valid xml.

Edit: I am trying to combine xml files which contain some Data and the "path" where the data is supposed to be written into one big xml file.

First i want to create the new xml from these "paths" and then write the data into the created nodes. So i don't have any structure to work with only the string which is created by combing the "paths" out of the xml files and separating them with "|" so i can split the string into each "path".

Upvotes: 0

Views: 140

Answers (1)

Mat
Mat

Reputation: 2072

As your input is not a standard format (at least non I'm aware of), you have to write your own parser.

I suggest you create an object (tree) first from the string:

class Node
{
List<Node> Children {get;set;}
}

Then you can use XmlSerializer to create the XML.

XmlSerializer serializer = XmlSerializer(typeof(Node));
using(TextWriter writer = new StreamWriter(filename))
{
    serializer.Serialize(writer, node1);
}

https://msdn.microsoft.com/de-de/library/system.xml.serialization.xmlserializer%28v=vs.110%29.aspx?f=255&MSPPError=-2147217396

Upvotes: 1

Related Questions