0

我试图将数据发布到只接受 XML 的 Web 应用程序。我已经在 c# 中创建了对象(如下所示),并且正在使用将XmlSerializer对象序列化为 XML,但无法弄清楚如何构造对象以获得接收应用程序所需的结果 XML:

所需的结果 XML

<recipients>
    <gsm messageId="clientmsgID1">number1</gsm>
    <gsm messageId="clientmsgID2">number2</gsm>
    <gsm messageId="clientmsgID3">number3</gsm>
    <gsm messageId="clientmsgID4">number4</gsm>
</recipients>

我的对象

public class recipients
{
    public List<gsm> gsm{ get; set; }

    public recipients()
    {
        gsm = new List<gsm>();
    }
}

public class gsm
{
    [XmlText]
    public string number { get; set; }

    [XmlAttribute]
    public string messageId{ get; set; }
}

我生成的 XML

<recipients>
    <gsm>
        <gsm messageId="clientmsgID1">number1</gsm>
    </gsm>
</recipients>
4

4 回答 4

3

使用 xsd.exe 并尝试传递上面显示的 xml 文件。这将创建一个 xsd 文件,使用此 xsd 创建一个 cs 类,然后在您的应用程序中使用该 cs 类,这将在序列化时创建相同的 XMl,例如

C:>xsd gsm.xml 其中 gsm.xml 将包含您在上面粘贴的 xml 标签
,然后
C:>xsd gsm.xsd /c 生成 cs 类

using System.Xml.Serialization;

public partial class recipients {
  private recipientsGsm[] itemsField;

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


public partial class recipientsGsm {
  private string messageIdField;
  private string valueField;

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

  /// <remarks/>
  [System.Xml.Serialization.XmlTextAttribute()]
  public string Value {
    get { return this.valueField; }
    set { this.valueField = value; }
  }
}
于 2013-10-21T11:15:22.657 回答
2

you only need to add

[System.Xml.Serialization.XmlElementAttribute("gsm", Form=System.Xml.Schema.XmlSchemaForm.Unqualified, IsNullable=true)]

at class recipients

public class recipients
{
    [System.Xml.Serialization.XmlElementAttribute("gsm", Form=System.Xml.Schema.XmlSchemaForm.Unqualified, IsNullable=true)]
    public List<gsm> gsm{ get; set; }

    public recipients()
    {
        gsm = new List<gsm>();
    }
}

and it should work

于 2013-10-21T11:41:43.867 回答
1

您可以使用属性[XmlElement("gsm")]标记您的属性gsm

类的完整列表:

public class recipients
{
    [XmlElement("gsm")]
    public List<gsm> gsm { get; set; }

    public recipients()
    {
        gsm = new List<gsm>();
    }
}

public class gsm
{
    [XmlText]
    public string number { get; set; }

    [XmlAttribute]
    public string messageId { get; set; }
}

示例代码:

var a = new recipients();
a.gsm.Add(new gsm() { messageId = "1", number = "aaa" });
a.gsm.Add(new gsm() { messageId = "2", number = "bbb" });
XmlSerializer serializer = new XmlSerializer(typeof(recipients));
XmlSerializerNamespaces ns = new XmlSerializerNamespaces();
ns.Add("", "");
serializer.Serialize(Console.Out, a, ns);

我的应用程序的输出:

<?xml version="1.0"?>
<recipients>
  <gsm messageId="1">aaa</gsm>
  <gsm messageId="2">bbb</gsm>
</recipients>
于 2013-10-21T11:53:14.920 回答
0

解决了

在尝试其他一些建议之前,我设法自己解决了这个问题,并认为无论如何我都应该在这里发布修复。

我做了recipients工具List<gsm>。完毕 :)

public class recipients: List<gsm>
{
    private List<gsm> gsms{ get; set; }

    public recipients()
    {
        gsms = new List<gsm>();
    }

    public IEnumerator<gsm> GetEnumerator()
    {
        return gsms.GetEnumerator();
    }
}

public class gsm
{
    [XmlText]
    public string number { get; set; }

    [XmlAttribute]
    public string messageId { get; set; }
}
于 2013-10-21T12:06:51.303 回答