Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
998 views
in Technique[技术] by (71.8m points)

xml serialization - C# - XML - Treat inner xml from a certain element as string

I have the following XML:

<Plan>
<Error>0</Error>
<Description>1</Description>
<Document>
  <ObjectID>06098INF1761320</ObjectID>
  <ced>109340336</ced>
  <abstract>DAVID STEVENSON</abstract>
  <ced_a />
  <NAM_REC />
  <ced_ap2 />
</Document>
</Plan>

I deserialize it with this:

[XmlRoot("Plan")]
    public class EPlan
    {
        [XmlElement("Error")]
        public string Error { get; set; }

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

        [XmlElement("Document")]
        public List<EDocument> Documents { get; set; }
    }

    public class EDocument
    {
        [XmlText]    
        public string Document { get; set; }   
    }

The issue is that I want the element "Document" to contain its inner XML as a single string, I mean, the object should have these values:

obj.Error = "0";
obj.Description = "1";
obj.Documents[0].Document = "<ObjectID>06098INF1761320</ObjectID><ced>109340336</ced><abstract>DAVID STEVENSON</abstract><ced_a /><NAM_REC /><ced_ap2 />";

But the way I mentioned before keeps retrieving a NULL "Document" property.

Is it possible to achieve the behaviour I want? Any help would be appreciated.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

XmlText expects a text node, but what you have are actually element nodes. I don't know if there's a direct way to do that, but you can have a XmlAnyElement node to collect the result of the deserialization, and afterwards you merge them in a single string if that's what you need, as shown in the example below.

    [XmlRoot("Plan")]
    public class EPlan
    {
        [XmlElement("Error")]
        public string Error { get; set; }

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

        [XmlElement("Document")]
        public List<EDocument> Documents { get; set; }
    }

    [XmlType]
    public class EDocument
    {
        private string document;

        [XmlAnyElement]
        [EditorBrowsable(EditorBrowsableState.Never)]
        public XmlElement[] DocumentNodes { get; set; }

        [XmlIgnore]
        public string Document
        {
            get
            {
                if (this.document == null)
                {
                    StringBuilder sb = new StringBuilder();
                    foreach (var node in this.DocumentNodes)
                    {
                        sb.Append(node.OuterXml);
                    }

                    this.document = sb.ToString();
                }

                return this.document;
            }
        }
    }

    static void Test()
    {
        string xml = @"<Plan>
  <Error>0</Error>
  <Description>1</Description>
  <Document>
    <ObjectID>06098INF1761320</ObjectID>
    <ced>109340336</ced>
    <abstract>DAVID STEVENSON</abstract>
    <ced_a />
    <NAM_REC />
    <ced_ap2 />
  </Document>
  <Document>
    <ObjectID>id2</ObjectID>
    <ced>ced2</ced>
    <abstract>abstract2</abstract>
    <ced_a />
    <NAM_REC />
    <ced_ap2 />
  </Document>
</Plan>";
        XmlSerializer xs = new XmlSerializer(typeof(EPlan));
        MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(xml));
        EPlan obj = xs.Deserialize(ms) as EPlan;
        Console.WriteLine(obj.Documents[0].Document);
    }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...