2009-01-28 102 views
1

我有一个web服务有一个类似于下面的输入对象。我怎样才能得到这个XML结构

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> 

对于我的生活,我似乎无法找到合适的属性(S)适用于获得这种格式。有人有建议吗?

回答

5

如果你知道你想要的结构,最简单的选择是从xml返回;写(在我的情况)的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; 
     } 
    } 
} 
1

寻找什么马克有一点,但我猜的Visual Studio 2005和2008

之间的差异,我需要添加以下到“Value”元素的声明。

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

它看起来像是有效的!