XML将属性反序列化为字典 [英] XML deserialize Attributes to dictionary

查看:121
本文介绍了XML将属性反序列化为字典的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

亲爱的,



如果我的术语不准确,请原谅我,这对我来说是个新领域。 :)

我在VB中实现了一个序列化为xml的类(实际上xml文件将被反序列化为类,但是)



所以XML:

 <   Universe  >  
< Jedi >
< 名称 = Yoda 年龄 = 1000 / >
< / Jed我 >
< / Universe >



结果类:

< pre lang =vb>< XmlRoot( Universe),Serializable()> _
公共 Universe
< XmlArray( Jedi)> _
< XmlArrayItem( )>
公共 as 列表( Person)
结束

公共 Person
< XmlAttributeAttribute( 名称)> _
公共名称 as string
< XmlAttributeAttribute( 年龄)> _
公共年龄 as 字符串
结束



到目前为止一直这么好......

现在我有一种情况,需要将一些未知数量的额外属性添加到某些人物中,就像



 <     名称  =  Obi Van   年龄  =  2000    isMaster   =     isAlive   =  no < span class =code-keyword>>  
< 名称 = Luke 年龄 = 300 isMaster = no >





所以我的想法是在我的Person Class中添加一个Dictionary(Of String,String)来收集所有的是...... - 将Xelement人员归属于。



首先,我不能在此时使用字典(作为调试) ging指向我...)我不知道如何将XElement的所有属性收集到List或一些进行进一步处理。



公共类人员
< XmlAttributeAttribute (名称) > _
Public Name as string
< XmlAttributeAttribute (年龄) > _
Public Age as String

<? ?? XmlattributesOfElement ??? >
public myAttributes as [Element of elements startwithis]
End Class





无论如何,我真的没有看到,如何获得Xelement ...(虽然我不得不用不同的读者实现一个功能,左右......)



实际上,如果有一个更好的方法来处理这个问题,我很感激任何替代方案!



任何提示都表示赞赏!

提前致谢,

Daniel

解决方案

你必须实现IXmlSerializable [ ^ ]并序列化/反序列化自己。



这篇文章展示了如何实现它:如何正确实现IXmlSerializable [ ^ ]



不难,XmlReader可让您访问属性,但必须在元素之间移动你自己,你也必须自己序列化(所以你放在你的属性上的属性变得毫无意义)。


.NET的字典不能序列化,所以你必须自己实现它...

  public   class 属性< ; K,V> 
{
public K Key
{
get ;
set ;
}

public V值
{
get ;
set ;
}
}
public class Person
{
public Person()
{
Properties = new List<属性< string,object>>();
}

public string 名称
{
get ;
set ;
}

[XmlElement( Propery)]
public 列表< Property< string,object>>属性
{
get ;
set ;
}
}



 oPerson.Name =  彼得; 
oPerson.Properties.Add( new 属性< string,object>()
{
Key = 年龄
值= 42
});
oPerson.Properties.Add( new 属性< string,object>()
{
Key = 性别
值= male
});



 <?  xml     version   =  1.0    >  
< >
< 名称 > Peter < / Name >
< Propery >
< > 年龄< / Key >
< > 42 < / Value >
< / Propery >
< Propery >
< > 性别< / Key >
< 价值 > male < / Value >
< / Propery >
< /人 >



这将为您提供一系列可以序列化的键值对...


Dear all,

Please forgive me if my terminology isn't accurate, it's a new field for me. :)
I have implemented a class in VB which is serialized to xml (actually the xml-file will be deserialized to the class, however)

So the XML:

<Universe>
<Jedi>
<Person Name="Yoda" Age="1000"/>
</Jedi>
</Universe>


resulting Class:

<XmlRoot ("Universe"), Serializable()> _
Public Class Universe
<XmlArray("Jedi")> _
<XmlArrayItem("Person")>
Public Persons as list(Of Person)
End Class

Public Class Person
<XmlAttributeAttribute ("Name")> _ 
Public Name as string
<XmlAttributeAttribute ("Age")> _ 
Public Age as String
End Class


So far so good...
Now I have a situation, where an unknown amount of additional Attributes needed to be added to some "Persons", just like

<Person Name="Obi Van" Age="2000" isMaster="yes" isAlive="no">
<Person Name="Luke" Age="300" isMaster="no">



So my idea was to add a Dictionary(Of String, String) to my Person Class to collect all the "is..."-Attributes of the Xelement Person into.

First of it, I cant use a Dictionary at this point (as the debugging pointed to me...) and I have no idea how to collect all attributes of an XElement to a List or some for further processing.

Public Class Person
<XmlAttributeAttribute ("Name")> _
Public Name as string
<XmlAttributeAttribute ("Age")> _
Public Age as String

<???XmlattributesOfElement???>
public myAttributes as [Attributes of Element startwith "is"]
End Class



Anyway, I don't really see, how to get the Xelement at all... (althought I had to implement a Function with a different reader, or so.... )

Actually, if there is a better way to handle this at all, I was thankful for any alternatives!

Any hints are appreciated!
Thanks in advance,
Daniel

解决方案

You will have to implement IXmlSerializable[^] in your Person class and serialize/deserialize yourself.

Here is an article that shows how to implement it: How to Implement IXmlSerializable Correctly[^]

Its not hard, the XmlReader gives you access to the attributes, but you have to move between elements yourself, and you also have to serialize yourself (so the attributes you put on your properties become meaningless).


Dictionaries of .NET are not serialize-able, so you have to implement it yourself...

public class Property<K, V>
{
    public K Key
    {
        get;
        set;
    }

    public V Value
    {
        get;
        set;
    }
}
public class Person
{
    public Person ( )
    {
        Properties = new List<Property<string, object>>( );
    }

    public string Name
    {
        get;
        set;
    }

    [XmlElement( "Propery" )]
    public List<Property<string, object>> Properties
    {
        get;
        set;
    }
}


oPerson.Name = "Peter";
oPerson.Properties.Add( new Property<string, object>( )
{
    Key = "Age",
    Value = 42
} );
oPerson.Properties.Add( new Property<string, object>( )
{
    Key = "Gender",
    Value = "male"
} );


<?xml version="1.0" ?>
<Person>
  <Name>Peter</Name>
  <Propery>
    <Key>Age</Key>
    <Value>42</Value>
  </Propery>
  <Propery>
    <Key>Gender</Key>
    <Value>male</Value>
  </Propery>
</Person>


This will give you a list of key-value pairs that can be serialize...


这篇关于XML将属性反序列化为字典的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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