.net (去)连载";“对象”;s

.net (去)连载";“对象”;s,.net,serialization,xml-serialization,.net,Serialization,Xml Serialization,我必须对以下类进行XML(反)序列化: 这将提供以下输出: <ArrayOfPropertyFilter xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"> <PropertyFilter> <AndOr>And</AndOr> <

我必须对以下类进行XML(反)序列化:

这将提供以下输出:

<ArrayOfPropertyFilter xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                       xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <PropertyFilter>
    <AndOr>And</AndOr>
    <LeftBracket>None</LeftBracket>
    <Property>17</Property>
    <Operator>Equal</Operator>
    <Value xsi:type="xsd:string">lll</Value>
    <RightBracket>None</RightBracket>
  </PropertyFilter>
</ArrayOfPropertyFilter>

及
没有一个
17
相等的
微光
没有一个
在反序列化之后,它会

我如何“告诉”序列化程序保持值“原样”,而不使用任何XML节点……(在具体情况下,值应该是“lll”,而不是包含文本“lll”的XMLNode)

编辑 Bellow是C#中的完整工作示例。输出是

值为='System.Xml.XmlNode[]'

使用系统;
使用System.IO;
使用系统文本;
使用System.Windows.Forms;
使用System.Xml;
使用System.Xml.Serialization;
命名空间Windows窗体应用程序13
{
公共部分类Form1:Form
{
公共表格1()
{
初始化组件();
}
私有void Form1\u加载(对象发送方、事件参数e)
{
PropertyFilter过滤器=新的PropertyFilter();
过滤器。和或=“Jora”;
filter.Value=“哈哈”;
filter.Property=15;
var xml=filter.SerializeToString();
XmlDocument xmlDoc=新的XmlDocument();
LoadXml(xml);
PropertyFilter cloneFilter=xmlDoc.Deserialize();
WriteLine(“值为='{0}',cloneFilter.Value);
}
}
公共类属性过滤器
{
公共字符串和或{get;set;}
公共字符串左括号{get;set;}
公共int属性{get;set;}
公共字符串运算符{get;set;}
公共对象值{get;set;}
公共字符串右括号{get;set;}
}
公共静态类Utils
{
公共静态字符串SerializeToString(此对象实例)
{
if(实例==null)
抛出新的ArgumentNullException(“实例”);
StringBuilder sb=新的StringBuilder();
StringWriter sw=新的StringWriter(sb);
XmlSerializer serializer=新XmlSerializer(
instance.GetType(),null,新类型[0],null,null);
serializer.Serialize(软件,实例);
使某人返回字符串();
}
公共静态T反序列化(此XmlDocument xmlDoc)
{
XmlNodeReader reader=新的XmlNodeReader(xmlDoc.DocumentElement);
XmlSerializer serializer=新的XmlSerializer(typeof(T));
object obj=序列化程序。反序列化(读取器);
T myT=(T)obj;
返回myT;
}
}
}
编辑2 为了强调安东的回答,第二个例子(更新了格罗的评论):

使用系统;
使用System.IO;
使用系统文本;
使用System.Windows.Forms;
使用System.Xml;
使用System.Xml.Serialization;
命名空间Windows窗体应用程序13
{
公共部分类Form1:Form
{
公共表格1()
{
初始化组件();
}
私有void Form1\u加载(对象发送方、事件参数e)
{
PropertyFilter过滤器=新的PropertyFilter();
过滤器。和或=“Jora”;
var obj=新的Hehe();
obj.Behehe=4526;
filter.Value=obj;
filter.Property=15;
var xml=filter.SerializeToString();
PropertyFilter cloneFilter=xml.Deserialize();
WriteLine(“值为='{0}',cloneFilter.Value);
}
}
公务舱呵呵
{
public int Behehe{get;set;}
公共重写字符串ToString()
{
返回string.Format(“behehe是{0}'”,behehe);
}
}
公共类属性过滤器
{
公共字符串和或{get;set;}
公共字符串左括号{get;set;}
公共int属性{get;set;}
公共字符串运算符{get;set;}
//[XmlElement(typeof(Hehe))]
公共对象值{get;set;}
公共字符串右括号{get;set;}
}
公共静态类Utils
{
公共静态字符串SerializeToString(此对象实例)
{
if(实例==null)
抛出新的ArgumentNullException(“实例”);
StringBuilder sb=新的StringBuilder();
XmlSerializer serializer=新XmlSerializer(instance.GetType(),null,新类型[0],null,null);
使用(StringWriter sw=新StringWriter(sb))
{
serializer.Serialize(软件,实例);
}
使某人返回字符串();
}
公共静态T反序列化(此字符串xmlString)
{
XmlSerializer serializer=新的XmlSerializer(typeof(T));
使用(StringReader sr=新的StringReader(xmlString))
{
返回(T)序列化程序。反序列化(sr);
}
}
}
}

好的,试试这个,但我不确定你想达到什么目的

public class PropertyFilter
{
    public string AndOr {get; set;}
    public string LeftBracket {get; set;}
    public int Property {get; set;}
    public string Operator {get; set;}
    public object Value {get; set;}
    public string RightBracket {get; set;}
}
    public void MyMethod()
    {
        using (System.IO.StreamReader reader = new System.IO.StreamReader(@"Input.xml"))
        {
            System.Xml.Serialization.XmlSerializer serializer = new XmlSerializer(typeof(PropertyFilter[]));
            PropertyFilter[] deserialized = (PropertyFilter[])serializer.Deserialize(reader);
        }
    }

我只是将示例XML放入
Input.XML
文件中。我希望这会有所帮助。

好的,试试这个,但我不确定你想要实现什么

public class PropertyFilter
{
    public string AndOr {get; set;}
    public string LeftBracket {get; set;}
    public int Property {get; set;}
    public string Operator {get; set;}
    public object Value {get; set;}
    public string RightBracket {get; set;}
}
    public void MyMethod()
    {
        using (System.IO.StreamReader reader = new System.IO.StreamReader(@"Input.xml"))
        {
            System.Xml.Serialization.XmlSerializer serializer = new XmlSerializer(typeof(PropertyFilter[]));
            PropertyFilter[] deserialized = (PropertyFilter[])serializer.Deserialize(reader);
        }
    }

我只是将示例XML放入
Input.XML
文件中。我希望这会有所帮助。

只要
Value
接受原始的、XSD定义的类型(如
string
int
)的值,或者
T
定义中提到的用户定义类型(
T
本身、其属性类型等)的值,Gree的解决方案就会起作用一旦需要反序列化不同类型的值,就必须使用
xmlementattribute
声明所有可能的
value
类型,例如

[XmlElement (typeof (string))]
[XmlElement (typeof (int))]
[XmlElement (typeof (MyType), Namespace = "http://example.com/schemas/my")]
public object Value { get ; set ; }

只要
Value
采用原始的、XSD定义的类型(如
string
int
)的值,或者
T
的定义中提到的用户定义类型(
T
本身、其属性类型等),Gree的解决方案就会起作用一旦需要反序列化不同于这些类型的值,就必须声明所有
[XmlElement (typeof (string))]
[XmlElement (typeof (int))]
[XmlElement (typeof (MyType), Namespace = "http://example.com/schemas/my")]
public object Value { get ; set ; }
public static class Utils
{
    public static string SerializeToString(this object instance)
    {
        if (instance == null)
            throw new ArgumentNullException("instance");

        StringBuilder sb = new StringBuilder();
        XmlSerializer serializer = new XmlSerializer(instance.GetType());

        using (StringWriter sw = new StringWriter(sb))
        {
            serializer.Serialize(sw, instance);
        }

        return sb.ToString();
    }

    public static T Deserialize<T>(this string xmlString)
    {
        XmlSerializer serializer = new XmlSerializer(typeof(T));
        using (StringReader sr = new StringReader(xmlString))
        {
            return (T)serializer.Deserialize(sr);
        }
    }
}
// serialize
var xml = filter.SerializeToString();

// deserialize
var cloneFilter = xml.Deserialize<PropertyFilter>();
public static T Deserialize<T>(this string xmlString)
{
    Type[] typesToInclude = GetAllPossibleTypes();

    XmlSerializer serializer = new XmlSerializer(typeof(T), typesToInclude);
    using (StringReader sr = new StringReader(xmlString))
    {
        return (T)serializer.Deserialize(sr);
    }
}
public static class Utils
{
    private static readonly Type[] _typesToInclude = GetPossibleUserTypes();

    private static Type[] GetPossibleUserTypes()
    {
        // this part should be changed to load types from the assembly
        // that contains your potential Value candidates
        Assembly assembly = Assembly.GetAssembly(typeof(PropertyFilter));

        // get public classes only
        return assembly.GetTypes().Where(t => t.IsPublic && !t.IsAbstract).ToArray();
    }

    public static string SerializeToString(this object instance)
    {
        if (instance == null)
            throw new ArgumentNullException("instance");

        var sb = new StringBuilder();
        var serializer = new XmlSerializer(instance.GetType(), _typesToInclude);

        using (StringWriter sw = new StringWriter(sb))
        {
            serializer.Serialize(sw, instance);
        }

        return sb.ToString();
    }

    public static T Deserialize<T>(this string xmlString)
    {
        var serializer = new XmlSerializer(typeof(T), _typesToInclude);
        using (StringReader sr = new StringReader(xmlString))
        {
            return (T)serializer.Deserialize(sr);
        }
    }
}