C# XML Deserialization. Read all inner text from node into string property

孤人 提交于 2020-01-02 11:45:11

问题


I am currently trying to modify my classes so that a my text property on my model contains all inner text of a certain node (text) node.

An example of the xml which is giving me issues is:

            <component>
                <section>
                    <title>Reason for Visit</title>
                    <text>
                        <content ID="ID3EZZKACA">No Reason for Visit was given.</content>
                    </text>
                </section>
            </component>

My goal is for my model's text property to have the following string:

"<content ID="ID0EAAKACA">No Reason for Visit was given.</content>"

currently my model looks like the following:

public partial class ComponentSection {
    //other model properties here
    private string textField;

    [System.Xml.Serialization.XmlTextAttribute()]
  public string text {
        get {
            return this.textField;
        }
        set {
            this.textField = value;
        }
    }
    //getters/setters for other properties here
}

So, I am currently trying to accomplish this by using the annotation [System.Xml.Serialization.XmlTextAttribute()] however when I do so, the text property is always coming through as null when the xml is deserialized.


回答1:


As I said in a comment, starting with serialization is often easier. For the XML above, here are some classes

public sealed class component
{
    public section section { get; set; }
}
public sealed class section
{
    public string title { get; set; }
    public text text { get; set; }
}
public sealed class text
{
    public content content { get; set; }
}
public sealed class content
{
    public string text { get; set; }
    public string ID { get; set; }
}

Then, modify the content class to control XML serialization:

public sealed class content
{
    [XmlText]
    public string text { get; set; }
    [XmlAttribute]
    public string ID { get; set; }
}

You can then serialize an instance with the following code:

        static string ToXmlString<T>(T t)
        {
            var serializer = new XmlSerializer(t.GetType());
            using (var sw = new System.IO.StringWriter())
            {
                serializer.Serialize(sw, t);
                return sw.ToString();
            }
        }
        static void Main(string[] args)
        {
            var c = new component { section = new section {
                            title = "Reason for Visit", text = new text { content = new content {
                            ID = "ID3EZZKACA", text = "No Reason for Visit was given." } } } };

            string s = ToXmlString(c);
            Console.WriteLine(s);
        }

The result is the following XML:

<?xml version="1.0" encoding="utf-16"?>
<component xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http
://www.w3.org/2001/XMLSchema">
  <section>
    <title>Reason for Visit</title>
    <text>
      <content ID="ID3EZZKACA">No Reason for Visit was given.</content>
    </text>
  </section>
</component>



回答2:


Use the XmlAnyElement attribute and define the text property as an XmlElement.

            [XmlAnyElement]
            public XmlElement text { get; set; }

This will cause the contents of the text element to be loaded into the text property.



来源:https://stackoverflow.com/questions/41269130/c-sharp-xml-deserialization-read-all-inner-text-from-node-into-string-property

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!