Reputation: 282
i have the following XML file:
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<a>
<b attr0="">
<c>
<d attr1="" attr2="">
<e>
<f/>
<g/>
<h/>
<i/>
</e>
</d>
<!-- ...more d's -->
</c>
</b>
<b>
<c>
<d attr1="" attr2="">
<e>
<f/>
<g/>
<h/>
<i/>
</e>
</d>
<!-- ...more d's -->
</c>
</b>
<!-- ...more b's -->
</a>
I want to deserialize it into C# objects, i am using the following classes:
Class a:
[XmlRoot(ElementName = "a")]
public class a
{
[XmlElement("b")]
List<b> bs = new List<b>();
}
Class b:
public class b
{
[XmlAttribute("attr0")]
String attr0{ get; set; }
[XmlElement("c")]
c c1 = new c();
}
Class c:
public class c
{
[XmlElement("d")]
List<d> ds = new List<d>();
}
Class d:
public class d
{
[XmlAttribute(AttributeName = "attr1")]
String attr1{ get; set; }
[XmlAttribute(AttributeName = "attr2")]
String attr2{ get; set; }
[XmlElement("e")]
List<e> es = new List<e>();
}
and Class e:
public class e
{
[XmlText]
String f { get; set; }
[XmlText]
String g { get; set; }
[XmlText]
String h { get; set; }
[XmlText]
String i { get; set; }
}
And with the following code i try to deserialize it:
public a deserialize()
{
XmlSerializer deserializer = new XmlSerializer(typeof(a));
System.IO.TextReader reader = new System.IO.StreamReader(@"C:\file.xml");
object obj = deserializer.Deserialize(reader);
a XmlData = (a)obj;
reader.Close();
return a;
}
Well right now, nothing is working. I was trying to add a XMLArray tag on it, but didn't work. You guys would do me a big favor for some good advice :)
Upvotes: 0
Views: 266
Reputation: 12616
There are two issues in your code.
1) Declare your class members' access modifiers that you want to de/serialize to public
like this. The default serialization implementation only works with public members.
[XmlRoot(ElementName = "a")]
public class a
{
[XmlElement("b")]
public List<b> bs = new List<b>();
}
2) You can't declare multiple [XmlText]
in a single object, in your case class e
. Change them to [XmlElement]
, instead.
public class e
{
[XmlElement]
public String f { get; set; }
[XmlElement]
public String g { get; set; }
[XmlElement]
public String h { get; set; }
[XmlElement]
public String i { get; set; }
}
Then it will work.
Upvotes: 2
Reputation: 307
XElement xmlFile= new XElement("a",
new XElement("b",
new XElement("c",
new XElement("d", new XAttribute("attr1"," ") , new XAttribute("attr2"," ")),
new XElement("e", new XElement("f"), new XElement("g"), new XElement("h"), new XElement("i")),
new XElement("c",
new XElement("d", new XAttribute("attr1"," ") , new XAttribute("attr2"," ")),
new XElement("e", new XElement("f"), new XElement("g"), new XElement("h"), new XElement("i")),
new XElement("Department", new XAttribute("Name","Automobile"))
),
new XElement("b",
new XElement("c",
new XElement("d", new XAttribute("attr1"," ") , new XAttribute("attr2"," ")),
new XElement("e", new XElement("f"), new XElement("g"), new XElement("h"), new XElement("i")),
new XElement("c",
new XElement("d", new XAttribute("attr1"," ") , new XAttribute("attr2"," ")),
new XElement("e", new XElement("f"), new XElement("g"), new XElement("h"), new XElement("i"))
)
); xmlFile.Save(@"D:\file.xml");
try this if it helps resolving your problem. And you can deserialize in the same manner i have serialized.
EDIT:
try to add the last line in your code too. I tried adding in the part of code section but it's not happening somehow.
Upvotes: 0