如何反序列化包含xml中的List的xmlelement [英] How to deserialize the xmlelement containing List in xml

查看:131
本文介绍了如何反序列化包含xml中的List的xmlelement的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用silverlight5和c#实现目标。我在下面有xml文件:

I am using silverlight5 and c# to achieve my targets. I have xml file below:

string xmlstring = 
<?xml version="1.0" encoding="utf-8" ?>
<parameter>
  <name>bands_amounts</name>
  <label>Bands Amounts</label>
  <unit></unit>
  <component>
    <type>List</type>
    <attributes>
      <type>Integer</type>
      <displayed>4</displayed>
      <add_remove>yes</add_remove> 
      <item>1 000 000</item>
      <item>5 000 000</item>
      <item>10 000 000</item>
      <item>20 000 000</item>
    </attributes>
    <attributes>
      <ccypair>XAUUSD</ccypair>
      <item>100</item>
      <item>500</item>
      <item>1000</item>
    </attributes>
  </component >
</parameter>

在反序列化中,我有3个课程。

On deserializing i got 3 classes.

 parameter.cs :
using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Runtime.Serialization.Json;
using System.Runtime;
using System.Xml.Serialization;
using System.Runtime.Serialization;
using System.IO;
using System.Collections.Generic;
using System.Diagnostics;


namespace Model.XML
{
   [XmlRoot("parameter")]
    public class parameter
    {
        public string name { get; set; }
        public string label { get; set; }

      [XmlElement("component")]
       public component component { get; set; }
    }
}

attribute.cs:
using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Runtime.Serialization.Json;
using System.Runtime;
using System.Xml.Serialization;
using System.Runtime.Serialization;
using System.IO;
using System.Collections.Generic;
using System.Diagnostics;
using System.Xml;


namespace Model.XML
{
    public class attributes
    {

        public string type { get; set; }    
        public string displayed { get; set; }
        public string add_remove { get; set; }
        public string ccypair { get; set; }
        public List<int> item { get; set; }

         public static void Main()
        {          
            string xmlstring = @"<?xml version='1.0' encoding='utf-8' ?> <parameter> <name>bands_amounts</name>   <label>Bands Amounts</label>   <unit>54</unit>  <component>    <type>List</type>    <attributes>      <type>Integer</type>     <displayed>4</displayed>    <add_remove>yes</add_remove>       <item>1 000 000</item>       <item>5 000 000</item>       <item>10 000 000</item>       <item>20 000 000</item>   </attributes>    <attributes>   <ccypair>XAUUSD</ccypair> <item>100</item> <item>500</item> <item>1000</item> </attributes>  </component > </parameter>";
            XmlSerializer serializer = new XmlSerializer(typeof(parameter));
            XmlReader reader = XmlReader.Create(new StringReader(xmlstring));
            var Object1 = (parameter)serializer.Deserialize(reader);
            foreach (var attrib in Object1.component.attribut)
            {
                Debug.WriteLine(attrib.type);
                Debug.WriteLine(attrib.item);

            }
        }  
    }
}

和第三个:

component.cs
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Runtime.Serialization.Json;
using System.Runtime;
using System.Xml.Serialization;
using System.Runtime.Serialization;
using System.IO;
using System.Collections.Generic;
using System.Diagnostics;
using System.Xml;

namespace Model.XML
{
    public class component
    {

        [XmlElement("attributes")]
        public List<attributes> attribut { get; set; }

    /*    public static void Main()
        {
            string xmlstring = @"<?xml version='1.0' encoding='utf-8' ?> <parameter> <name>bands_amounts</name>   <label>Bands Amounts</label>   <unit>54</unit>  <component>    <type>List</type>    <attributes>      <type>Integer</type>     <displayed>4</displayed>    <add_remove>yes</add_remove>       <item>1 000 000</item>       <item>5 000 000</item>       <item>10 000 000</item>       <item>20 000 000</item>   </attributes>    <attributes>   <ccypair>XAUUSD</ccypair> <item>100</item> <item>500</item> <item>1000</item> </attributes>  </component > </parameter>";

            XmlSerializer deserializer = new XmlSerializer(typeof(parameter));          
            XmlReader reader = XmlReader.Create(new StringReader(xmlstring));

            var Object1 = (parameter)deserializer.Deserialize(reader);
            foreach (var attrib in Object1.component.attribut)
            {
                Debug.WriteLine(attrib.item);
              //  Debug.WriteLine(Object1.name);

            }
        }   */
    }
}

我的问题是,当我尝试执行 Debug.WriteLine(attrib.attrib。属性类中的在执行Debug.WriteLine(attrib.ccypair)时, ccypair也显示为null。 (可能是因为该xml的结构包含两个 < attributes> ..< / attributes< attributes> ..< / attributes> )。 如何获取它们?因为我的下一步是使用它们显示GUI,所以我将需要它们的值。

My problem is i am not able to see "1 000 000" and "5 000 000" and "10 000 000" on debugging when i tried to do Debug.WriteLine(attrib.item); in attributes class also "ccypair" shows null on doing Debug.WriteLine(attrib.ccypair); (may be because the structure of this xml contains two <attributes>..</attributes> <attributes>..</attributes> ). how to get them ? Because my next step is to display a GUI using them so i will need their values. Could some one please as i am a beginer.

我需要做类似的事情吗?

Do I need to do something like :

 [XmlArray("component"), XmlArrayItem("attributes", typeof(attributes))]
        public List<attributes> component
        {
            get { return (_attributes); }
            set { _attributes = value; }
        }
        private List<attributes> _attributes = new List<attributes>();

因为包含 1 000 000, 5 000 000等的项目是里面的属性?请向我解释我是初学者

because this "item" which contains "1 000 000" ,"5 000 000" etc. are inside the "attribute" ? Please explain me i am beginner

推荐答案

使用以下代码,即使您可以存储xml,也可以序列化和反序列化对象>

using below code you can serialize and deserialize you object even you can stored your xml

SerializeObject(objDividendList, filename);

objDividendList = DeSerializeObject>(文件名);

objDividendList = DeSerializeObject>(filename);

public void SerializeObject<T>(T serializableObject, string fileName)
    {
        if (serializableObject == null) { return; }

        try
        {
            XmlDocument xmlDocument = new XmlDocument();
            XmlSerializer serializer = new XmlSerializer(serializableObject.GetType());
            using (MemoryStream stream = new MemoryStream())
            {
                serializer.Serialize(stream, serializableObject);
                stream.Position = 0;
                xmlDocument.Load(stream);
                xmlDocument.Save(fileName);
                stream.Close();
            }
        }
        catch (Exception ex)
        {
            //Log exception here
            log.Error("SerializeObject ", ex);

        }
    }

    public T DeSerializeObject<T>(string fileName)
    {
        if (string.IsNullOrEmpty(fileName)) { return default(T); }

        T objectOut = default(T);

        try
        {
            string attributeXml = string.Empty;

            XmlDocument xmlDocument = new XmlDocument();
            xmlDocument.Load(fileName);
            string xmlString = xmlDocument.OuterXml;

            using (StringReader read = new StringReader(xmlString))
            {
                Type outType = typeof(T);

                XmlSerializer serializer = new XmlSerializer(outType);
                using (XmlReader reader = new XmlTextReader(read))
                {
                    objectOut = (T)serializer.Deserialize(reader);
                    reader.Close();
                }

                read.Close();
            }
        }
        catch (Exception ex)
        {
            //Log exception here
            log.Error("DeSerializeObject ", ex);

        }

        return objectOut;
    }

这篇关于如何反序列化包含xml中的List的xmlelement的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆