代码之家  ›  专栏  ›  技术社区  ›  Mitchel Sellers

如何获取此XML结构

  •  1
  • Mitchel Sellers  · 技术社区  · 17 年前

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

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

    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>
    

    就我的一生而言,我似乎找不到合适的属性来应用以获得此格式。有人有什么建议吗?

    2 回复  |  直到 17 年前
        1
  •  5
  •   Marc Gravell    17 年前

    foo.xml

    xsd foo.xml
    xsd foo.xsd /classes
    

    然后看看 foo.cs 看看如何做到这一点;事实证明,您只需使用 [System.Xml.Serialization.XmlTextAttribute()] .

    //------------------------------------------------------------------------------
    // <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
  •  1
  •   Mitchel Sellers    17 年前

    看看Marc有什么,但我猜VisualStudio2005和2008有什么不同。

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

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