user1682718
user1682718

Reputation: 57

c# deserialization objects

I got a xml like below

        string input =
@"<g1:Person xmlns:g1=""http://api.google.com/staticInfo/"">
    <g1:Id>005008</g1:Id>
    <g1:Infolist>
 <g1:Info><g1:Title>a</g1:Title></g1:Info>    
<g1:Info<g1:Title>b</g1:Title></g1:Info>     
<g1:Info><g1:Title>c</g1:Title></g1:Info>
<g1:overview>there are three chaters.</g1:overview>
  </g1:Infolist>
    <g1:age>23</g1:age>
  </g1:Person>";

I define the object , But I don't know where to put the / Person/Infolist/overview. this property how to define. where to put.

   [XmlRoot(ElementName = "Person", Namespace = "http://api.google.com/staticInfo/")]
    public class Person
    {

        public int Id { get; set; }

        public int age { get; set; }

        [XmlElement(ElementName = "Infolist", Namespace = "http://api.google.com/staticInfo/")]
        public List<Info> Infolist {get;set; }
    }

    public class Info
    {
        public int Title { get; set; }
    }

Upvotes: 0

Views: 124

Answers (1)

Aaron Anodide
Aaron Anodide

Reputation: 17180

//------------------------------------------------------------------------------
// <auto-generated>
//     This code was generated by a tool.
//     Runtime Version:4.0.30319.269
//
//     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=4.0.30319.1.
// 


/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.0.30319.1")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true, Namespace="http:=//api.google.com/staticInfo/")]
[System.Xml.Serialization.XmlRootAttribute(Namespace="http:=//api.google.com/staticInfo/", IsNullable=false)]
public partial class Person {

    /// <remarks/>
    public ushort Id;

    /// <remarks/>
    public PersonInfolist Infolist;

    /// <remarks/>
    public byte age;
}

/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.0.30319.1")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true, Namespace="http:=//api.google.com/staticInfo/")]
public partial class PersonInfolist {

    /// <remarks/>
    [System.Xml.Serialization.XmlElementAttribute("Info")]
    public string[] Info;

    /// <remarks/>
    public string overview;
}

Steps:

  • paste your Xml into a new Xml file in Visual studio
  • Click Xml -> Create Schema
  • Save schema file to disk
  • Open visual studio command prompt
  • execute command: xsd /classes /fields so.xsd

That's probably one of many ways to get a result. I like letting tools generate my code - they never make mistakes.

Upvotes: 1

Related Questions