C# how to deserialize an xml tag embedded in text?

 ̄綄美尐妖づ 提交于 2019-12-11 01:33:12

问题


I am trying to deserialize the output of .NET's XML doc comment using an XmlSerializer. For reference, the output of xml documentation looks like:

<?xml version="1.0"?>
<doc>
    <assembly>
        <name>Apt.Lib.Data.Product</name>
    </assembly>
    <members>
        <member name="P:MyNamespace.MyType.MyProperty">
            <summary>See <see cref="T:MyNamespace.MyOthertype"/> for more info</summary>
        </member>
        ...
    </members>
</doc>

The object I'm using to generate the serializer is:

    [XmlRoot("doc")]
    public class XmlDocumentation
    {
        public static readonly XmlSerializer Serializer = new XmlSerializer(typeof(XmlDocumentation));

        [XmlElement("assembly")]
        public AssemblyName Assembly { get; set; }
        [XmlArray("members")]
        [XmlArrayItem("member")]
        public List<Member> Members { get; set; }

        public class AssemblyName
        {
            [XmlElement("name")]
            public string Name { get; set; }
        }

        public class Member
        {
            [XmlAttribute("name")]
            public string Name { get; set; }
            [XmlElement("summary")]
            public string Summary { get; set; }
        }
}

The problem is when the serializer encounters the embedded see cref tag. In that case the serializer throws the following exception:

System.InvalidOperationException : There is an error in XML document (147, 27). ----> System.Xml.XmlException : Unexpected node type Element. ReadElementString method can only be called on elements with simple or empty content. Line 147, position 27.

How can I capture the entire content of the summary tag as a string during deserialization?


回答1:


The cref tag itself contains illegal characters. Specifically <, > can't be embedded in the contents of an XML element. You should sanitize the strings before they are serialized or deserialized.

You can do something like this if you need to be able to apply specific rules to how certain characters are escaped or substituted:

    string ScrubString(string dirty)
    {
        char[] charArray = dirty.ToCharArray();
        StringBuilder strBldr = new StringBuilder(dirty.Length);

        for (int i = 0; i < charArray.Length; i++)
        {
           if(IsXmlSafe(charArray[i]))
           {
              strBldr.Append(charArray[i]);
           }
           else
           {
              //do something to escape or replace that character. 
           }
        }
        retrun strBldr.ToString();
    }


    bool IsXmlSafe(char c)
    {
       int charInt = Convert.ToInt32(c);

       return charInt == 9
           || charInt == 13
           || (charInt >= 32    && charInt <= 9728)
           || (charInt >= 9983  && charInt <= 55295)
           || (charInt >= 57344 && charInt <= 65533)
           || (charInt >= 65536 && charInt <= 1114111);
    }

You can also use some of the approaches here to just remove any illegal character using regex:

Invalid Characters in XML



来源:https://stackoverflow.com/questions/17091545/c-sharp-how-to-deserialize-an-xml-tag-embedded-in-text

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