claunia / plist-cil

C#/.NET parser for Apple and GnuStep Property List (aka plist), based on Java's dd-plist
Other
54 stars 17 forks source link

[Xml Reading problem] A text after "CData section" is not enough string. #29

Open Ushio opened 6 years ago

Ushio commented 6 years ago

Hi,

I trying following xml.

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
    <key><![CDATA[b%]]]]><![CDATA[>def]]></key>
    <string><![CDATA[b%]]]]><![CDATA[>def]]></string>
</dict>
</plist>

I expected "b%]]>def" string and key. But I got "b%]]" It is strange.

Because, following CData parsing code is not enough. https://github.com/claunia/plist-cil/blob/master/plist-cil/XmlPropertyListParser.cs#L201

So, I propose following implementation.


        static string GetNodeTextContents(XmlNode n)
        {
            if (n.NodeType == XmlNodeType.Text || n.NodeType == XmlNodeType.CDATA)
            {
                string content = n.Value; //This concatenates any adjacent text/cdata/entity nodes
                return content ?? "";
            }
            if (n.HasChildNodes)
            {
                XmlNodeList children = n.ChildNodes;

                string text = "";
                foreach (XmlNode child in children)
                {                 
                    //Skip any non-text nodes, like comments or entities
                    if (child.NodeType == XmlNodeType.Text || child.NodeType == XmlNodeType.CDATA)
                    {
                        if(child.Value != null) {
                            text += child.Value;
                        }
                    }
                }
                return text;
            }
            return "";
        }

But it is hard for me to construct testing environment this project... :( How can I contribute ?

3breadt commented 6 years ago

The method GetNodeTextContents could also be replaced by getting the value of the XmlNode.InnerText property.