代码之家  ›  专栏  ›  技术社区  ›  Ravisha

无法将XML反序列化为对象

  •  2
  • Ravisha  · 技术社区  · 15 年前

    我有以下代码,其中我试图序列化和反序列化StringResource类的对象。 请注意resource1.stringxml=它来自资源文件。如果传递strelemet.outerxml,则从反序列化对象获取该对象,但如果传递resource1.stringxml,则会收到以下异常

    {“<string xmlns=''>不是预期的。”}系统。异常{系统。无效操作异常}

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

    <string id=“1”delete=“true”/>

    2 回复  |  直到 15 年前
        1
  •  1
  •   RameshVel    15 年前

    问题是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
  •  0
  •   Darin Dimitrov    15 年前

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

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