c# - 我怎样才能得到这个 XML 结构

标签 c# .net xml web-services asmx

我有一个网络服务,它有一个类似于下面的输入对象。

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 结构。

<Demographics>
    <Demographic Name="String">
        <value>string</value>
    </Demographic>
    <Demographic Name="String">
        <value>string</value>
    </Demographic>
</Demographics>

我需要把它放进去

<Demographics>
    <Demographic Name="String">string</Demographic>
    <Demographic Name="String">string</Demographic>
</Demographics>

在我的生活中,我似乎无法找到合适的属性来应用以获取此格式。有人有什么建议吗?

最佳答案

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

xsd foo.xml
xsd foo.xsd /classes

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

这是 xsd 输出:

//------------------------------------------------------------------------------
// <auto-generated>
//     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.
// </auto-generated>
//------------------------------------------------------------------------------

using System.Xml.Serialization;

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


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

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

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

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

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

关于c# - 我怎样才能得到这个 XML 结构,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/486867/

相关文章:

c# - 获取字符串中模式的匹配项

.net - 您认为 ASP.NET WebForms 是一种不好的做法吗?

c# - 类似于 .Net/C# 的 Jericho HTML 的解析器

c# - 无法编译,因为 Visual Studio 正在使用我的 DLL

c# - 如何识别对象何时为空?

.net - Bitmap.Height和VerticalResolution之间的区别

java - 解析自定义 xml 配置文件

php - 如何将 XML 文件附加到另一个 XML 文件

xml - 通过菜单按钮调用首选项时强制关闭消息

c# - Windows 窗体 : Play sound, 但不是从头开始