XML deserialization

I have the following xml file.

<a> <b> <c>val1</c> <d>val2</d> </b> <b> <c>val3</c> <d>val4</d> </b> <a> 

I want to deserialize this into a class, and I want to access them with the objects of the created class. I am using C #. I can deserialize and get the value into an object of class a ( <a> tag). but how to access the <b> value from this object? I did the following encoding:

  [Serializable ()]
 [XmlRoot ("a")]
 public class a
 {
     [XmlArray ("a")]
     [XmlArrayItem ("b", typeof (b))]
     public b [] bb {get;  set;  }
 }

 [Serializable ()]
 public class b
 {
     [XmlElement ("c")]
     public string c {get;  set;  }
     [XmlElement ("d")]
     public string d {get;  set;  }    
 }
 class program
 {
         static void Main (string [] args)
         {

             ai = null;
             string path = "test.xml";

             XmlSerializer serializer = new XmlSerializer (typeof (a));

             StreamReader reader = new StreamReader (path);
             i = (a) serializer.Deserialize (reader);
             reader.Close ();
             // i want to print all b tags here
             Console.Read ();
         }
     }
+6
c # xml serialization
source share
3 answers

For this you can make the following change

 public class a { [XmlElement("b")] public b[] bb{ get; set; } } 

Using the XmlElement attribute in the array, you essentially tell the serializer that the elements of the array should be serialized / deserialized as direct children of the current element.

Here is a working example, I put the XML in a string to make the example on my own.

 using System; using System.IO; using System.Xml.Serialization; namespace ConsoleApplication1 { class Program { static void Main(string[] args) { string xml = @"<a> <b> <c>val1</c> <d>val2</d> </b> <b> <c>val3</c> <d>val4</d> </b> </a>"; XmlSerializer xs = new XmlSerializer(typeof(a)); ai = (a)xs.Deserialize(new StringReader(xml)); if (i != null && i.bb != null && i.bb.Length > 0) { Console.WriteLine(i.bb[0].c); } else { Console.WriteLine("Something went wrong!"); } Console.ReadKey(); } } [XmlRoot("a")] public class a { [XmlElement("b")] public b[] bb { get; set; } } public class b { [XmlElement("c")] public string c { get; set; } [XmlElement("d")] public string d { get; set; } } } 
+7
source share

If in doubt about creating xml serialization classes, I can find the easiest way to solve the problem:

  • dump all your dummy data to an XML file
  • run xsd.exe to create the .xsd schema file
  • run xsd.exe in the schema file to create the class file

I wrote a short guide on this issue on a blog some time ago: http://www.diaryofaninja.com/blog/2010/05/07/make-your-xml-stronglytyped-because-you-can-and-its-easy

it takes less than a minute and you can easily set things up there. XSD.exe is your friend

+2
source share

I believe that the easiest way to solve the problem is to change the definition of classes a and b to the following

 public class b { public string c { get; set; } public string d { get; set; } } [XmlRoot(Namespace="", ElementName="a")] public class a : List<b> { } 

then your program will work. Optionally, you can add the attribute [XmlRoot (Namespace = "", ElementName = "b")] to class b

0
source share

All Articles