【问题标题】:How can I get this XML Structure我怎样才能得到这个 XML 结构
【发布时间】:2009-01-28 08:24:10
【问题描述】:

我有一个 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 结构。

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

在我的一生中,我似乎找不到合适的属性来应用来获得这种格式。有人有什么建议吗?

【问题讨论】:

    标签: c# .net xml web-services asmx


    【解决方案1】:

    如果你知道你想要的结构,最简单的选择是从 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;
            }
        }
    }
    

    【讨论】:

      【解决方案2】:

      看看 Marc 有什么,但我猜 Visual Studio 2005 和 2008 之间的区别。

      我需要在“Value”元素的声明中添加以下内容。

      [System.Xml.Serialization.XmlText()]
      public string Value { get; set; }
      

      它看起来有效!

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2014-06-28
        • 2022-01-06
        • 1970-01-01
        • 2022-10-24
        • 2018-02-22
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多