0

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".

2
  • "Folder"? If that string represents a folder structure it would be easier to generate XML directly from that structure instead of that string. Otherwise you'll have to parse the string, recreate the tree and then serialize it Commented Aug 6, 2018 at 8:05
  • check this similar question stackoverflow.com/questions/14798854/c-xml-adding-new-nodes Commented Aug 6, 2018 at 9:16

1 Answer 1

1

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

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

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.