not able to Deserialize xml into object
I am having following peice of code ,where in i am trying to serialize and deserailize object of StringResource class. Please note Resource1.stringXml = its coming from resource file.If i pass strelemet.outerXMl i get the object from Deserialize object ,but if i pass Resource1.stringXml i am getting following exception
{"< STRING xmlns=''> was not expected."} System.Exception {System.InvalidOperationException}
 class Program
{
    static void Main(string[] args)
    {
        StringResource str = new StringResource();
        str.DELETE = "CanDelete";
        str.ID= "23342";
        XmlElement strelemet = SerializeObjectToXmlNode (str);
        StringResource strResourceObject = DeSerializeXmlNodeToObject<StringResource>(Resource1.stringXml);
        Console.ReadLine();
    }
    public static T DeSerializeXmlNodeToObject<T>(string objectNodeOuterXml)
    {
        try
        {
            TextReader objString开发者_JAVA技巧sTextReader = new StringReader(objectNodeOuterXml);
            XmlSerializer stringResourceSerializer = new XmlSerializer(typeof(T),string.Empty);
            return (T)stringResourceSerializer.Deserialize(objStringsTextReader);
        }
        catch (Exception excep)
        {
            return default(T);
        }
    }
    public static XmlElement SerializeObjectToXmlNode(object obj)
    {
        using (MemoryStream memoryStream = new MemoryStream())
        {
            try
            {
                XmlSerializerNamespaces xmlNameSpace = new XmlSerializerNamespaces();
                xmlNameSpace.Add(string.Empty, string.Empty);
                XmlWriterSettings writerSettings = new XmlWriterSettings();
                writerSettings.CloseOutput = false;
                writerSettings.Encoding = System.Text.Encoding.UTF8;
                writerSettings.Indent = false;
                writerSettings.OmitXmlDeclaration = true;
                XmlWriter writer = XmlWriter.Create(memoryStream, writerSettings);
                XmlSerializer xmlserializer = new XmlSerializer(obj.GetType());
                xmlserializer.Serialize(writer, obj, xmlNameSpace);
                writer.Close();
                memoryStream.Position = 0;
                XmlDocument serializeObjectDoc = new XmlDocument();
                serializeObjectDoc.Load(memoryStream);
                return serializeObjectDoc.DocumentElement;
            }
            catch (Exception excep)
            {
                return null;
            }
        }
    }
}
public class StringResource
{
    [XmlAttribute]
    public string DELETE;
    [XmlAttribute]
    public string ID;
}
< STRING ID="1" DELETE="True" />
Problem is a name mismatch in your XML root node and your class
< STRING ID="1" DELETE="True" />  -- STRING here
public class StringResource       -- StringResource Here.
Add xmlroot attribute to your class, and will see what happens
[XmlRoot( "STRING " )]
public class StringResource 
{ 
    [XmlAttribute] 
    public string DELETE; 
    [XmlAttribute] 
    public string ID; 
} 
Wel it's obvious Resource1.stringXml does not contain a correct XML which could be deserialized to a StringResource object. The XML should look like this:
<StringResource DELETE="CanDelete" ID="23342" />
 
         加载中,请稍侯......
 加载中,请稍侯......
      
精彩评论