LINQ to XML question: how to convert non-logical XML to Object?

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
Have a XML:
<div style="color:Black;background-color:White; <pre>
<span style="color:Blue; <<span style="color:#A31515; chapter<span style="color:Blue; >
<span style="color:Blue; <<span style="color:#A31515; header <span style="color:Red; name<span style="color:Blue; =<span style="color:Black; "<span style="color:Blue; 1<span style="color:Black; " <span style="color:Blue; />
<span style="color:Blue; <<span style="color:#A31515; param <span style="color:Red; value<span style="color:Blue; =<span style="color:Black; "<span style="color:Blue; 1-123<span style="color:Black; "<span style="color:Blue; >
<span style="color:Blue; <<span style="color:#A31515; param <span style="color:Red; value<span style="color:Blue; =<span style="color:Black; "<span style="color:Blue; 1-456<span style="color:Black; "<span style="color:Blue; >
<span style="color:Blue; <<span style="color:#A31515; param <span style="color:Red; value<span style="color:Blue; =<span style="color:Black; "<span style="color:Blue; 1-789<span style="color:Black; "<span style="color:Blue; >
<span style="color:Blue; <<span style="color:#A31515; header <span style="color:Red; name<span style="color:Blue; =<span style="color:Black; "<span style="color:Blue; 2<span style="color:Black; " <span style="color:Blue; />
<span style="color:Blue; <<span style="color:#A31515; param <span style="color:Red; value<span style="color:Blue; =<span style="color:Black; "<span style="color:Blue; 2-123<span style="color:Black; "<span style="color:Blue; >
<span style="color:Blue; <<span style="color:#A31515; param <span style="color:Red; value<span style="color:Blue; =<span style="color:Black; "<span style="color:Blue; 2-456<span style="color:Black; "<span style="color:Blue; >
<span style="color:Blue; <<span style="color:#A31515; param <span style="color:Red; value<span style="color:Blue; =<span style="color:Black; "<span style="color:Blue; 2-789<span style="color:Black; "<span style="color:Blue; >
<span style="color:Blue; </<span style="color:#A31515; chapter<span style="color:Blue; >

[/code]
<span id="x_result_box" lang="en <span>But not with the correct structure, as this:

<div style="color:Black;background-color:White; <pre>
<span style="color:Blue; <<span style="color:#A31515; chapter<span style="color:Blue; >
<span style="color:Blue; <<span style="color:#A31515; header <span style="color:Red; name<span style="color:Blue; =<span style="color:Black; "<span style="color:Blue; 1<span style="color:Black; "<span style="color:Blue; >
<span style="color:Blue; <<span style="color:#A31515; param <span style="color:Red; value<span style="color:Blue; =<span style="color:Black; "<span style="color:Blue; 1-123<span style="color:Black; "<span style="color:Blue; >
<span style="color:Blue; <<span style="color:#A31515; param <span style="color:Red; value<span style="color:Blue; =<span style="color:Black; "<span style="color:Blue; 1-456<span style="color:Black; "<span style="color:Blue; >
<span style="color:Blue; <<span style="color:#A31515; param <span style="color:Red; value<span style="color:Blue; =<span style="color:Black; "<span style="color:Blue; 1-789<span style="color:Black; "<span style="color:Blue; >
<span style="color:Blue; </<span style="color:#A31515; header<span style="color:Blue; >
<span style="color:Blue; <<span style="color:#A31515; header <span style="color:Red; name<span style="color:Blue; =<span style="color:Black; "<span style="color:Blue; 2<span style="color:Black; "<span style="color:Blue; >
<span style="color:Blue; <<span style="color:#A31515; param <span style="color:Red; value<span style="color:Blue; =<span style="color:Black; "<span style="color:Blue; 2-123<span style="color:Black; "<span style="color:Blue; >
<span style="color:Blue; <<span style="color:#A31515; param <span style="color:Red; value<span style="color:Blue; =<span style="color:Black; "<span style="color:Blue; 2-456<span style="color:Black; "<span style="color:Blue; >
<span style="color:Blue; <<span style="color:#A31515; param <span style="color:Red; value<span style="color:Blue; =<span style="color:Black; "<span style="color:Blue; 2-789<span style="color:Black; "<span style="color:Blue; >
<span style="color:Blue; </<span style="color:#A31515; header<span style="color:Blue; >
<span style="color:Blue; </<span style="color:#A31515; chapter<span style="color:Blue; >

[/code]
And Chapter object is:
<div style="color:Black;background-color:White; <pre>
<span style="color:Blue; class Chapter
{
<span style="color:Blue; public List<Header> Headers;
}

<span style="color:Blue; class Header
{
<span style="color:Blue; public <span style="color:Blue; string Name { <span style="color:Blue; get; <span style="color:Blue; set; }
<span style="color:Blue; public List<Param> Params;
}

<span style="color:Blue; class Param
{
<span style="color:Blue; public <span style="color:Blue; string Value { <span style="color:Blue; get; <span style="color:Blue; set; }
}

[/code]
How to correctly transform first variant of XML to object "Chapter", using
LINQ ? In DOM i usу foreach:

<div style="color:Black;background-color:White; <pre>
<span style="color:Blue; foreach (XmlNode node <span style="color:Blue; in chapter) {<br/>
<span style="color:Blue; if (node.Name == <span style="color:#A31515; "header") <span style="color:Green; // current Header is...<br/>
<span style="color:Blue; else <span style="color:Blue; if(node.Name == <span style="color:#A31515; "param") <span style="color:Green; // add object Param to current Header<br/>
}<br/>

[/code]
p.S. Sorry for my english.


View the full article
 
Back
Top