Nisha
Nisha

Reputation: 1853

how to read binary data ¬ ( ) in xml using c# generated by windows

how to read binary data ¬ ( ) in xml using c# generated by windows . I am trying to access the List out the registry keys in the windows reg . There are binary data popping up in string value . I am able to serialize it using xml serialization ,to (&#x2) but I am not able to de-serialize it back ? Can anyone explain me what is going wrong and why ? help me to fix this one . Many thanks :))) Error while reading is:

System.InvalidOperationException was unhandled
Message="There is an error in XML document (235, 28)." Read From file:

public diagnostics readregkey(diagnostics diagnostics, string filename)
{
    diagnostics dia = null;
    using (System.IO.StreamReader sr = 
        new System.IO.StreamReader(filename, Encoding.Unicode))
    {
        System.Xml.Serialization.XmlSerializer x = 
            new System.Xml.Serialization.XmlSerializer(typeof(diagnostics));
        dia = x.Deserialize(sr) as diagnostics;
    }
    return dia;
}

Write to file:

public static void  WriteRegKey(diagnostics diagnostics, string filename)
{
    diagnostic.regKeys.Add(key(Registry.LocalMachine, sKeyGravitas));
    diagnostic.regKeys.Add(key(Registry.CurrentUser, sKeyGravitas));
    using (System.IO.StreamWriter sw = 
        new System.IO.StreamWriter(filename,false, Encoding.Unicode))
    {
        System.Xml.Serialization.XmlSerializer x = 
            new System.Xml.Serialization.XmlSerializer(typeof(diagnostics));
        x.Serialize(sw, diagnostics);
    }
}

Upvotes: 1

Views: 9137

Answers (2)

Duarte Patrício
Duarte Patrício

Reputation: 143

I had this problem also and, in order to help the next one, I leave here what I used to solve it.

As lavinio said, XML cannot represent every character. Therefore, using the table he provided I wrote a method, very similar to another answer in another question: https://stackoverflow.com/a/641632/2315604, that I call before before serializing.

private string XmlCharWhiteList(string p)
{
        StringBuilder stringBuilder = new StringBuilder();
        foreach(char c in p)
        {
            if (c == 0x0009 || c == 0x000A || c == 0x000D ||
                (c >= 0x0020 && c <= 0xD7FF) ||
                (c >= 0xE000 && c <= 0xFFFD))
                stringBuilder.Append(c);
            else
                stringBuilder.Append(' ');
        }
        return stringBuilder.ToString();
}

And, since .NET 4.0, you can use XmlConvert.IsXmlChar https://msdn.microsoft.com/en-us/library/system.xml.xmlconvert.isxmlchar%28v=vs.110%29.aspx

Upvotes: 2

lavinio
lavinio

Reputation: 24309

XML files cannot represent every character, even using the &#x; notation.

See http://www.w3.org/TR/REC-xml/#charsets

XML 1.1, which is not widely used, can support all characters except NUL.

But for 0x0002, expressing it as &#x2; in XML simply isn't the right format.

The usual way of handling embedded binary data is to convert it to base-64.

Upvotes: 3

Related Questions