How to deserialize this xml file?

I have this Xml file and I need to Deserialize it back to the class. The problem is this: what is the correct structure / design of this class, given that the number of Xml elements (RowInfo) is not constant? Xml file:

<?xml version="1.0"?> <SomeObject xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" > <Layers> <Layer Id="0"> <RowInfo>1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1</RowInfo> <RowInfo>1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,1</RowInfo> <RowInfo>1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1</RowInfo> <RowInfo>1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1</RowInfo> <RowInfo>1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1</RowInfo> <RowInfo>1,1,1,0,0,1,1,1,1,1,1,1,1,1,1,0,0,1,1,1</RowInfo> <RowInfo>1,1,1,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1</RowInfo> <RowInfo>1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1</RowInfo> <RowInfo>1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1</RowInfo> <RowInfo>1,1,1,1,1,1,1,1,1,0,0,1,1,1,1,1,1,1,1,1</RowInfo> <RowInfo>1,1,1,0,0,1,1,1,1,1,1,1,1,1,1,1,0,0,1,1</RowInfo> <RowInfo>1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1</RowInfo> <RowInfo>1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1</RowInfo> <RowInfo>1,1,1,1,1,1,1,1,1,0,0,1,1,1,1,1,1,1,1,1</RowInfo> <RowInfo>1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1</RowInfo> <RowInfo>1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1</RowInfo> <RowInfo>1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1</RowInfo> <RowInfo>1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1</RowInfo> <RowInfo>1,1,1,0,0,1,1,1,1,1,1,1,1,1,0,0,1,0,0,1</RowInfo> <RowInfo>1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,1,1,1,1</RowInfo> </Layer> </Layers> </SomeObject> 

Appreciate your help. Thanks.

Edit1 : also considering that (Layers) can contain more than one layer.

+4
source share
6 answers

This should work the way you want:

 public class SomeObject { public List<Layer> Layers { get; set; } } public class Layer { [XmlAttribute] public int Id { get; set; } [XmlElement("RowInfo")] public List<RowInfo> RowInfos { get; set; } } public class RowInfo { [XmlText] public string Info { get; set; } // you'll need to parse the list of ints manually } 

The only difference is coding, but you should be able to get around it.

+2
source

Something like the following names should be changed to protect the innocent:

Class structure after comment

 public class SomeObject { public List<Layer> Layers {get;set;} } public class Layer { public int Id {get;set;} public List<RowInfo> RowInfos {get;set;} } public class RowInfo { public List<Row> Rows {get;set;} } public class Row { public int RowData {get;set;} } 
+3
source

If the RowInfo value RowInfo not constant, use List in your class.

+1
source

I would say using LINQ-to-Xml. Take a constructor on your object that can take an xlement, then do Something along the lines

 public class YourObject() { public IEnumerable<Layer> Layers { get; set; } public int Id { get; set; } public YourObj(XElement x) { this.Id = int.Parse(x.Attribute("Id").ToString()); this.Layers = from layer in x.Elements("Layer") select new Layer(layer); } } var objs = (from c in XElement.Load("your.xml").Elements("layer") select new YourObject(c)).ToList() ; 
+1
source
 XmlSerializer serializer = new XmlSerializer(typeof(SomeObject)); 

you can use XmlSerializer with the following code and call Deserialize :)

 //------------------------------------------------------------------------------ // <auto-generated> // This code was generated by a tool. // Runtime Version:2.0.50727.4952 // // Changes to this file may cause incorrect behavior and will be lost if // the code is regenerated. // </auto-generated> //------------------------------------------------------------------------------ using System.Xml.Serialization; // // This source code was auto-generated by xsd, Version=2.0.50727.3038. // /// <remarks/> [System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "2.0.50727.3038")] [System.SerializableAttribute()] [System.Diagnostics.DebuggerStepThroughAttribute()] [System.ComponentModel.DesignerCategoryAttribute("code")] [System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true)] [System.Xml.Serialization.XmlRootAttribute(Namespace="", IsNullable=false)] public partial class SomeObject { private SomeObjectLayers layersField; /// <remarks/> public SomeObjectLayers Layers { get { return this.layersField; } set { this.layersField = value; } } } /// <remarks/> [System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "2.0.50727.3038")] [System.SerializableAttribute()] [System.Diagnostics.DebuggerStepThroughAttribute()] [System.ComponentModel.DesignerCategoryAttribute("code")] [System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true)] public partial class SomeObjectLayers { private SomeObjectLayersLayer layerField; /// <remarks/> public SomeObjectLayersLayer Layer { get { return this.layerField; } set { this.layerField = value; } } } /// <remarks/> [System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "2.0.50727.3038")] [System.SerializableAttribute()] [System.Diagnostics.DebuggerStepThroughAttribute()] [System.ComponentModel.DesignerCategoryAttribute("code")] [System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true)] public partial class SomeObjectLayersLayer { private decimal[] rowInfoField; private int idField; private bool idFieldSpecified; /// <remarks/> [System.Xml.Serialization.XmlElementAttribute("RowInfo")] public decimal[] RowInfo { get { return this.rowInfoField; } set { this.rowInfoField = value; } } /// <remarks/> [System.Xml.Serialization.XmlAttributeAttribute()] public int Id { get { return this.idField; } set { this.idField = value; } } /// <remarks/> [System.Xml.Serialization.XmlIgnoreAttribute()] public bool IdSpecified { get { return this.idFieldSpecified; } set { this.idFieldSpecified = value; } } } 
+1
source

Place an order using the tool XSD.exe, which comes with visual studio and allows you to generate code from an XML file.

You should see it in the program files menu next to visual studio.

0
source

Source: https://habr.com/ru/post/1341315/


All Articles