首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >c#反序列化对象

c#反序列化对象
EN

Stack Overflow用户
提问于 2012-09-28 09:58:19
回答 1查看 117关注 0票数 0

我得到了如下所示的xml

代码语言:javascript
复制
        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放在哪里。此属性如何定义。放在哪里。

代码语言:javascript
复制
   [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; }
    }
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-09-28 10:02:27

代码语言:javascript
复制
//------------------------------------------------------------------------------
// <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;
}

步骤:

  • 在Visual studio中将Xml粘贴到新的Xml文件中
  • 单击Xml ->创建架构
  • 将架构文件保存到磁盘
  • 打开visual studio命令prompt
  • execute命令:xsd /classes /fields so.xsd <代码>H212<代码>F213

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

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/12632588

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档