繁体   English   中英

C#反序列化对象

[英]c# deserialization objects

我有一个像下面的xml

        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>";

我定义了对象,但是我不知道将/ Person / Infolist / overview放在哪里。 这个属性怎么定义。 放在哪里。

   [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; }
    }
//------------------------------------------------------------------------------
// <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;
}

脚步:

  • 将您的Xml粘贴到Visual Studio中的新Xml文件中
  • 单击Xml->创建架构
  • 将架构文件保存到磁盘
  • 打开Visual Studio命令提示符
  • 执行命令: xsd /classes /fields so.xsd

那可能是获得结果的许多方法之一。 我喜欢让工具生成我的代码-它们从不犯错误。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM