Ashley Kilgour
Ashley Kilgour

Reputation: 1258

Deserialize odd XML formant

I am working with a odd XML formatted file, that I have never seen before. (I am pretty sure this is not XML and the just give me a file called download.xml, I will change question if someone tells me what it is)

I get it as a flat text file, that I have to load, not from a stream.

It is generated from some third party application that I can not change.

I cant figure out how to deserialize this into any kind of object.

I dont care what it is, as long as its not a string. Here is a fake dataset in its format

<table name="tbl_user" entire="Y">
  <columns>
     <column name="user_id" class="java.lang.Long" type-id="-5" db-type="bigint" />
     <column name="name" class="java.lang.String" length="255" type-id="12" db-type="varchar" />
     <column name="surnname" class="java.lang.String" length="255" type-id="12" db-type="varchar" />  
   </columns>
   <row><v>1</v><v> John</v><v>Lennon</v> </row>
   <row><v>2</v><v>Paul</v><v>McCartney</v></row>
   <row><v>3</v><v>George</v><v>Harrison</v></row>
   <row><v>4</v><v>Ringo</v><v>Starr</v></row>
</table>

I have played around with XDocument, XNode, DataTables, XmlSerializer but keep getting twisted in knots.

Upvotes: 0

Views: 44

Answers (1)

Dan Wilson
Dan Wilson

Reputation: 4047

This is valid XML that can be deserialized into an object.

All I did was generate a class using a free online tool (http://xmltocsharp.azurewebsites.net/).

You can also use the xsd.exe utility.

Data types can be modified as needed (e.g. strings that should be integers).

void Main()
{
    var xml = @"
  <table name=""tbl_user"" entire=""Y"">
     <columns>
        <column name=""user_id"" class=""java.lang.Long"" type-id=""-5"" db-type=""bigint"" />
        <column name=""name"" class=""java.lang.String"" length=""255"" type-id=""12"" db-type=""varchar"" />
        <column name=""surnname"" class=""java.lang.String"" length=""255"" type-id=""12"" db-type=""varchar"" />  
     </columns>
     <row><v>1</v><v> John</v><v>Lennon</v> </row>
     <row><v>2</v><v>Paul</v><v>McCartney</v></row>
     <row><v>3</v><v>George</v><v>Harrison</v></row>
     <row><v>4</v><v>Ringo</v><v>Starr</v></row>
  </table>";

    var serializer = new XmlSerializer(typeof(Table));
    var table = serializer.Deserialize(new MemoryStream(System.Text.Encoding.UTF8.GetBytes(xml))) as Table;
    Console.WriteLine($"{table.Columns.Column.Count} columns");
    Console.WriteLine($"{table.Row.Count} rows");

    // Output:
    // 3 columns
    // 4 rows

}

[XmlRoot(ElementName = "column")]
public class Column
{
    [XmlAttribute(AttributeName = "name")]
    public string Name { get; set; }
    [XmlAttribute(AttributeName = "class")]
    public string Class { get; set; }
    [XmlAttribute(AttributeName = "type-id")]
    public string Typeid { get; set; }
    [XmlAttribute(AttributeName = "db-type")]
    public string Dbtype { get; set; }
    [XmlAttribute(AttributeName = "length")]
    public string Length { get; set; }
}

[XmlRoot(ElementName = "columns")]
public class Columns
{
    [XmlElement(ElementName = "column")]
    public List<Column> Column { get; set; }
}

[XmlRoot(ElementName = "row")]
public class Row
{
    [XmlElement(ElementName = "v")]
    public List<string> V { get; set; }
}

[XmlRoot(ElementName = "table")]
public class Table
{
    [XmlElement(ElementName = "columns")]
    public Columns Columns { get; set; }
    [XmlElement(ElementName = "row")]
    public List<Row> Row { get; set; }
    [XmlAttribute(AttributeName = "name")]
    public string Name { get; set; }
    [XmlAttribute(AttributeName = "entire")]
    public string Entire { get; set; }
}

Upvotes: 2

Related Questions