【问题标题】:not able to Deserialize xml into object无法将 xml 反序列化为对象
【发布时间】:2010-03-22 21:09:31
【问题描述】:

我有以下代码,我正在尝试序列化和反序列化 StringResource 类的对象。 请注意 Resource1.stringXml = 它来自资源文件。如果我通过 strelemet.outerXMl 我从反序列化对象中获取对象,但如果我通过 Resource1.stringXml 我得到以下异常

{" 不是预期的。"} 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 objStringsTextReader = 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;
}

【问题讨论】:

  • 你也可以包含你的 XML 字符串吗? XML中似乎有一些问题......

标签: c# xml serialization


【解决方案1】:

问题是您的 XML 根节点和您的类中的名称不匹配

< STRING ID="1" DELETE="True" />  -- STRING here
public class StringResource       -- StringResource Here.

xmlroot 属性添加到您的类中,然后看看会发生什么

[XmlRoot( "STRING " )]
public class StringResource 
{ 
    [XmlAttribute] 
    public string DELETE; 
    [XmlAttribute] 
    public string ID; 
} 

【讨论】:

    【解决方案2】:

    很明显Resource1.stringXml 不包含可以反序列化为StringResource 对象的正确XML。 XML 应如下所示:

    <StringResource DELETE="CanDelete" ID="23342" />
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-05-10
      • 1970-01-01
      • 2014-11-30
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多