I have the following XML data that I wish to build into a tree structure?
<DATA>
<NODES>
<NODE>
<ID>1</ID>
<CONTENT>Foo</CONTENT>
<ChildrenIds>
<Child>2</Child>
<Child>3</Child>
</ChildrenIds>
<Level>1</Level>
</NODE>
<NODE>
<ID>2</ID>
<CONTENT>Foo</CONTENT>
<ChildrenIds>
<Child>4</Child>
</ChildrenIds>
</NODE>
<NODE>
<ID>3</ID>
<CONTENT>oo</CONTENT>
<ChildrenIds>
<Child>5</Child>
</ChildrenIds>
</NODE>
<NODE>
<ID>4</ID>
<CONTENT>Doo</CONTENT>
<ChildrenIds/>
</NODE>
<NODE>
<ID>5</ID>
<CONTENT>Koo</CONTENT>
<ChildrenIds/>
</NODE>
</NODES>
</DATA>
What's the best way to parse this into a tree structure using LINQ?
I'm assuming that I'll firstly need to start with a tree object such as the below:
public class Tree
{
public Tree()
{
ChildrenNodes = new List<TreeStructure>();
}
public List<Tree> ChildrenNodes { get; set; }
public int Id { get; set; }
public string Content {get; set;}
}
usign system.runtime.serialization
[datacontract]
public class Tree
{
public Tree()
{
ChildrenNodes = new List<TreeStructure>();
}
[datamember]
public List<Tree> ChildrenNodes { get; set; }
[datamember]
public int Id { get; set; }
[datamember]
public string Content {get; set;}
}
void serialize(Tree tree,string filename)
{
datacontractserializer serializer = new datacontracetserializer (typeof(Tree));
system.xml.xmltextwriter writer = new xmltextwriter(filename,Encoding.UTF32);
serializer.WriteObject(writer,tree);
writer.close();
}
void deserialize(ref Tree tree,string filename)
{
xmltextreader reader = new xmltextreader*new streamreader(filename, Encoding.UTF32));
DataContracetSerializer deser = new datacontracetserializer(typeof(Tree));
trr = (Tree)deserializer.readObject(reader);
reader.close()
}
select the encoding
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.