当前位置:  开发笔记 > 编程语言 > 正文

我怎样才能获得这个XML结构

如何解决《我怎样才能获得这个XML结构》经验,为你挑选了1个好方法。

我有一个Web服务,其输入对象类似于以下内容.

public class MyInput
{
    [System.Xml.Serialization.XmlArrayItem("Demographic")]
    public DemographicsInfo[] Demographics {get; set;}
}

使用像这样的DemographicsInfo类的定义.

public class DemographicsInfo
{
    [System.Xml.Serialization.XmlAttributeAttribute()]
    public string Name { get; set; }
    public string Value { get; set; }
}

现在,这会生成这样的XML结构.


    
        string
    
    
        string
    

我需要把它搞定


    string
    string

对于我的生活,我似乎无法找到适用于获取此格式的适当属性.有人有建议吗?



1> Marc Gravell..:

如果你知道你想要的结构,最简单的选择是从xml返回; 将xml写入文件(foo.xml在我的例子中),然后(在命令行):

xsd foo.xml
xsd foo.xsd /classes

然后看看foo.cs它是如何完成的; 事实证明,您只需使用标记值[System.Xml.Serialization.XmlTextAttribute()].

这是xsd输出:

//------------------------------------------------------------------------------
// 
//     This code was generated by a tool.
//     Runtime Version:2.0.50727.3053
//
//     Changes to this file may cause incorrect behavior and will be lost if
//     the code is regenerated.
// 
//------------------------------------------------------------------------------

using System.Xml.Serialization;

// 
// This source code was auto-generated by xsd, Version=2.0.50727.3038.
// 


/// 
[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 Demographics {

    private DemographicsDemographic[] itemsField;

    /// 
    [System.Xml.Serialization.XmlElementAttribute("Demographic", Form=System.Xml.Schema.XmlSchemaForm.Unqualified, IsNullable=true)]
    public DemographicsDemographic[] Items {
        get {
            return this.itemsField;
        }
        set {
            this.itemsField = value;
        }
    }
}

/// 
[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 DemographicsDemographic {

    private string nameField;

    private string valueField;

    /// 
    [System.Xml.Serialization.XmlAttributeAttribute()]
    public string Name {
        get {
            return this.nameField;
        }
        set {
            this.nameField = value;
        }
    }

    /// 
    [System.Xml.Serialization.XmlTextAttribute()]
    public string Value {
        get {
            return this.valueField;
        }
        set {
            this.valueField = value;
        }
    }
}

推荐阅读
殉情放开那只小兔子
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有