cómo leer datos binarios ¬ (?) en xml usando c # generado por 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 no se controló
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;
}

Escribir en archivo:

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);
    }
}

preguntado el 09 de marzo de 12 a las 14:03

Have you any example code / error messages we can use to help you? -

Show us your exact code for serializing it out, and later back in again? This appears not to be an isolated problem: marc.info/?l=xalan-dev&m=98865838020552&w=2 -

Isn't this the same as your other question, stackoverflow.com/questions/9631122/… ?? -

2 Respuestas

XML files cannot represent every character, even using the &#x; notación.

Vea 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  in XML simply isn't the right format.

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

respondido 09 mar '12, 15:03

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

contestado el 23 de mayo de 17 a las 12:05

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.