0

我在序列化 XML 元素时遇到问题,特别是删除数组/集合的根标记

 public Class Part
    {
        public Guid Id { get; set; }
        [System.Xml.Serialization.XmlElementAttribute(IsNullable = false)]
        [DataMember(IsRequired = true)]
        public string PartNumber { get; set; }

        [System.Xml.Serialization.XmlElementAttribute(IsNullable = false)]
        [DataMember(IsRequired = true)]
        public string PartName { get; set; }

        [System.Xml.Serialization.XmlElementAttribute(IsNullable = true)]
        public string Description { get; set; }

        [System.Xml.Serialization.XmlElementAttribute(IsNullable = false)]
        public DateTime LastUpdated { get; set; }

        [XmlElement()]
        public string[] PartDetails{ get; set; }
    }

wcf 将其序列化为

 <b:Part>
            <b:PartDetail xmlns:c="http://schemas.microsoft.com/2003/10/Serialization/Arrays">
              <c:string>ABC</c:string>
            </b:PartDetail>
            <b:PartDetail xmlns:c="http://schemas.microsoft.com/2003/10/Serialization/Arrays">
              <c:string>DEF</c:string>
            </b:PartDetail>
            <b:Description> JGHGS SGHSGH SJGHSJG</b:Description>
            <b:Id>740ead2d-84e8-4da0-9115-28dea5f0bd28</b:Id>
            <b:LastUpdated>2012-11-30</b:LastUpdated>
            <b:PartName>AAA BBB CCC DDDD</b:PartName>
            <b:PartNumber>1</b:PartNumber>
          </b:Part>

我需要什么

 <b:Part>
                <b:PartDetail xmlns:c="http://schemas.microsoft.com/2003/10/Serialization/Arrays">ABC</b:PartDetail>
                <b:PartDetail xmlns:c="http://schemas.microsoft.com/2003/10/Serialization/Arrays">DEF</b:PartDetail>             

                <b:Description> JGHGS SGHSGH SJGHSJG</b:Description>
                <b:Id>740ead2d-84e8-4da0-9115-28dea5f0bd28</b:Id>
                <b:LastUpdated>2012-11-30</b:LastUpdated>
                <b:PartName>AAA BBB CCC DDDD</b:PartName>
                <b:PartNumber>1</b:PartNumber>
              </b:Part>

我在 SO 中发现了类似的问题,但没有得到任何明确的答案(可能不明白)如何将它与 WCF 一起使用。

基本上我想要

<ArrayPropertyName>ABC</ArrayPropertyName>
<ArrayPropertyName>DEF</ArrayPropertyName>
<ArrayPropertyName>GHI</ArrayPropertyName>

代替

<ArrayPropertyName>
<string> ABC</string>
<string> DEF</string>
<string> GHI</string>
</ArrayPropertyName>

是否可以在 wcf 发送 xml 序列化数据之前搜索和删除标签?

让我知道问题是否清楚。

4

1 回答 1

0

您需要创建一个 PartDetail 类来完成此操作,例如

public class PartDetail
{
    [XmlText()] 
    public string value;
}    

public Class Part
{

     [XmlElement("PartDetail")]
     public PartDetail[] PartDetails { get; set; }

}
于 2012-12-28T19:45:30.330 回答