【问题标题】:XML serialization to object collectionXML 序列化到对象集合
【发布时间】:2012-10-21 12:26:57
【问题描述】:

我想将一个 xml 字符串序列化为对象集合,但我只能获取第一个对象。如果我在 xml 中添加更多对象,则会出错。不知道我错过了什么。我尝试将类型声明为 Emp[]

这是我的两个“Emp”xml 字符串

string empsworking = "<?xml version='1.0' encoding='utf-8'?><Emp><EmpInfo><Code>vv</Code><FirstName>van</FirstName><LastName>sa</LastName><Destination>sc</Destination></EmpInfo><EmpInfo><Code>rr</Code><FirstName>ro</FirstName><LastName>sa</LastName><Destination>ph</Destination></EmpInfo></Emp>";

         string empsNotworking = "<?xml version='1.0' encoding='utf-8'?><Emp><EmpInfo><Code>vv</Code><FirstName>van</FirstName><LastName>sa</LastName><Destination>sc</Destination></EmpInfo></Emp>";

我的课看起来像

[System.Xml.Serialization.XmlRootAttribute(Namespace = "", IsNullable = false)]
public class Emp
{
    /// <remarks/>
   public EmpInfo EmpInfo { get; set; }
    public IEnumerator GetEnumerator()
    {
        throw new NotImplementedException();
    }

}

/// <remarks/>
[XmlRoot(ElementName = "EmpInfo")] 
public class EmpInfo
{

    /// <remarks/>
    public string Code;

    /// <remarks/>
    public string FirstName;

    /// <remarks/>
    public string LastName;

    /// <remarks/>
    public string Destination;
}

我要序列化的代码是

           StringReader stream = null;
       XmlTextReader reader = null;
        Emp empprofile;
       try
       {
           // serialise to object
           XmlSerializer serializer = new XmlSerializer(typeof(Emp));
           stream = new StringReader(emps); // read xml data
           reader = new XmlTextReader(stream);  // create reader
           // covert reader to object
           empprofile = (Emp)serializer.Deserialize(reader);
       }
       catch
       {
           return null;
       }
       finally
       {
           if (stream != null) stream.Close();
           if (reader != null) reader.Close();
       }

我只能通过 empworking 读取/获取对象。 如何将其作为“EmpInfo”的集合?请指导!

【问题讨论】:

    标签: xml serialization xmlserializer


    【解决方案1】:

    试试这样:

    [TestFixture]
    public class SerializeTest
    {
        [Test]
        public void SerializeEmpTest()
        {
            EmpCollection empCollection = new EmpCollection()
                {
                    new EmpInfo() {Code = "1", FirstName = "Anita"}, 
                    new EmpInfo() {Code = "1", FirstName = "Johan"}
                };
            string xmlString = empCollection.GetXmlString();
        }
    }
    
    [XmlType, XmlRoot]
    public class EmpCollection : List<EmpInfo>
    {
    
    }
    
    [XmlType]
    public class EmpInfo
    {
        public string Code;
    
        public string FirstName;
    
    }
    
    public static class Extenstion
    {
        public static string GetXmlString<T>(this T objectToSerialize)
        {
            XmlSerializer xmlSerializer = new XmlSerializer(objectToSerialize.GetType());
            StringBuilder stringBuilder = new StringBuilder();
            string xml;
            using (var xmlTextWriter = new XmlTextWriter(new StringWriter(stringBuilder)))
            {
                xmlSerializer.Serialize(xmlTextWriter, objectToSerialize);
                xml = stringBuilder.ToString();
            }
            return xml;
        }
    }
    

    【讨论】:

    • 你的类定义就像一个魅力,我能够得到一个 EmpInfo 对象的列表。我不必更改序列化代码。谢谢!我怎样才能把它提升到一个新的水平?我的意思是我可以阅读嵌套对象集合。例如,如果我想将联系人作为 EmpInfo 中的集合。
    • vv vansasc888 99999999rr rosaph888999
    • 以上是我的意思的xml。从数据的角度来看,这不是一个完美的例子,但这就是结构。而 Contact 将是另一个类。
    • 我认为这对于 cmets 来说太长了。如果我理解正确,如果您将 Contacts 设为 List,我在上面发布的示例可以满足您的要求
    • 是的...我有点困惑 public class EmpInfo { public EmpInfo() { Contacts= new List(); } 公共字符串代码;公共字符串名字;公共列表 联系人 { 获取;放; } } 我试过 public List Contact { get;放; } 但联系人不会被填充。
    猜你喜欢
    • 1970-01-01
    • 2012-01-11
    • 2010-11-30
    • 2020-02-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-09-24
    相关资源
    最近更新 更多