How do I use XmlSerializer to insert an xml string

前端 未结 4 1335
伪装坚强ぢ
伪装坚强ぢ 2020-12-10 15:15

I have defined the following class:

public class Root
{
    public string Name;
    public string XmlString;
}

and created an object:

相关标签:
4条回答
  • 2020-12-10 15:20

    You can limit the custom serialization to just the element that needs special attention like so.

    public class Root
    {
        public string Name;
    
        [XmlIgnore]
        public string XmlString
        {
            get
            {
                if (SerializedXmlString == null)
                    return "";
                return SerializedXmlString.Value;
            }
            set
            {
                if (SerializedXmlString == null)
                    SerializedXmlString = new RawString();
                SerializedXmlString.Value = value;
            }
        }
    
        [XmlElement("XmlString")]
        [Browsable(false)]
        [EditorBrowsable(EditorBrowsableState.Never)]
        public RawString SerializedXmlString;
    }
    
    public class RawString : IXmlSerializable
    {
        public string Value { get; set; }
    
        public XmlSchema GetSchema()
        {
            return null;
        }
    
        public void ReadXml(System.Xml.XmlReader reader)
        {
            this.Value = reader.ReadInnerXml();
        }
    
        public void WriteXml(System.Xml.XmlWriter writer)
        {
            writer.WriteRaw(this.Value);
        }
    }
    
    0 讨论(0)
  • 2020-12-10 15:20

    You can (ab)use the IXmlSerializable interface an XmlWriter.WriteRaw for that. But as garethm pointed out you then pretty much have to write your own serialization code.

    using System;
    using System.Xml;
    using System.Xml.Schema;
    using System.Xml.Serialization;
    
    namespace ConsoleApplicationCSharp
    {
      public class Root : IXmlSerializable
      {
        public string Name;
        public string XmlString;
    
        public Root() { }
    
        public void WriteXml(System.Xml.XmlWriter writer)
        {
          writer.WriteElementString("Name", Name);
          writer.WriteStartElement("XmlString");
          writer.WriteRaw(XmlString);
          writer.WriteFullEndElement();
        }
    
        public void ReadXml(System.Xml.XmlReader reader) { /* ... */ }
        public XmlSchema GetSchema() { return (null); }
        public static void Main(string[] args)
        {
          Root t = new Root
          {
            Name = "Test",
            XmlString = "<Foo>bar</Foo>"
          };
          System.Xml.Serialization.XmlSerializer x = new XmlSerializer(typeof(Root));
          x.Serialize(Console.Out, t);
          return;
        }
      }
    }
    

    prints

    <?xml version="1.0" encoding="ibm850"?>
    <Root>
      <Name>Test</Name>
      <XmlString><Foo>bar</Foo></XmlString>
    </Root>
    
    0 讨论(0)
  • 2020-12-10 15:34

    I would be very surprised if this was possible. Suppose it was possible for you to do this - what would happen if you had malformed XML in the property - everything would just break.

    I expect that you will either need to write your own serialization for this case, or make it so that the XmlString field is a structure that contains a foo field.

    0 讨论(0)
  • 2020-12-10 15:42

    try this:

    public class Root
    {
        public string Name;
        public XDocument XmlString;
    }
    
    Root t = new Root 
             {  Name = "Test", 
                XmlString = XDocument.Parse("<Foo>bar</Foo>")
             };
    
    0 讨论(0)
提交回复
热议问题