2009-01-28 6 views
1

J'ai un service Web qui a un objet d'entrée semblable au suivant.Comment puis-je obtenir cette structure XML

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

Avec la définition de la classe DemographicsInfo comme ceci.

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

Actuellement, ceci génère une structure XML comme celle-ci.

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

Je dois obtenir dans ce

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

Pour la vie de moi, je ne peux pas sembler trouver l'attribut approprié (s) à appliquer pour obtenir ce format. Quelqu'un a-t-il des conseils?

Répondre

5

Si vous connaissez la structure que vous voulez, l'option la plus simple est de travailler à partir du xml; écrire le xml dans un fichier (dans mon cas), puis (à la ligne de commande):

xsd foo.xml 
xsd foo.xsd /classes 

Alors regardez foo.cs pour voir comment il peut être fait; il s'avère que vous marquez simplement la valeur avec [System.Xml.Serialization.XmlTextAttribute()].

est ici la sortie 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

Vous cherchez un peu sur ce que Marc a, mais je devine une différence entre Visual Studio 2005 et 2008.

je besoin d'ajouter ce qui suit à la déclaration de l'élément "Value".

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

Il semble que cela fonctionne!

Questions connexes