【问题标题】:Deserialize part of xml into string将部分xml反序列化为字符串
【发布时间】:2014-10-23 09:46:08
【问题描述】:

是否可以反序列化以下 XML:

<MyObject><Test>Hi hello</Test><Something><Else><With><SubItems count='5'>hello world</SubItems></With></Else></Something></MyObject>

进入这个对象:

public class MyObject {
    public string Test { get; set; }
    public string Something { get; set; }
}

这是预期的输出(目前失败,XmlException: Unexpected node type Element. ReadElementString method can only be called on elements with simple or empty content. Line 1, position 50.

[TestMethod]
public void TestDeserialization()
{
    var s = "<MyObject><Test>Hi hello</Test><Something><Else><With><SubItems count='5'>hello world</SubItems></With></Else></Something></MyObject>";

    var o = s.DeSerialize<MyObject>();
    Assert.AreEqual("Hi hello", o.Test);
    Assert.AreEqual("<Else><With><SubItems count='5'>hello world</SubItems></With></Else>", o.Something);
}

public static class Xml
{
    public static T DeSerialize<T>(this string xml) where T : new()
    {
        if (String.IsNullOrEmpty(xml))
        {
            return new T();
        }
        var xmlSer = new XmlSerializer(typeof(T));
        using (var stream = new StringReader(xml))
            return (T)xmlSer.Deserialize(stream);
    }
}

【问题讨论】:

  • 是的,有可能。你说你的代码失败了——为什么?您也没有显示DeSerialize 的代码,所以我们怎么能提供帮助?

标签: c# xml deserialization


【解决方案1】:

一种选择可能是实现IXmlSerializable,因此您可以手动将内部 Xml 读入Something 属性:

public class MyObject: IXmlSerializable 
{
    public string Test { get; set; }
    public string Something { get; set; }

    public System.Xml.Schema.XmlSchema GetSchema() { return null; }

    public void ReadXml(System.Xml.XmlReader reader)
    {       
        if (reader.MoveToContent() == XmlNodeType.Element && reader.LocalName == "MyObject")
        {           
            Test = reader["Test"];
            if (reader.ReadToDescendant("Something"))
            {
                Something = reader.ReadInnerXml();
            }
        }                   
    }

    public void WriteXml(System.Xml.XmlWriter writer)
    {
        throw new NotImplementedException();
    }   
}

public static class Program
{
    public static void Main()
    {   
        var myObjectXmlString = "<MyObject><Test>Hi hello</Test><Something><Else><With><SubItems count='5'>hello world</SubItems></With></Else></Something></MyObject>";    
        var myObject =(MyObject) new XmlSerializer(typeof(MyObject)).Deserialize(new StringReader(myObjectXmlString));
        Console.WriteLine(myObject.Something);
    }
}

另一种选择可能是将Something 转换为XmlElement 类型的属性,因此Xml 的内部随机片段将被序列化为Xml 而不是字符串(您仍然可以通过获取其OuterXml 将其作为字符串检索):

public class MyOtherObject 
{
    public string Test { get; set; }    
    public XmlElement Something { get; set; }
    public string SomethingString
    {
        get { return Something.OuterXml; }
    }
}

public static class Program
{
    public static void Main()
    {   
        var otherObjectXmlString = "<MyOtherObject><Test>Hi hello</Test><Something><Else><With><SubItems count='5'>hello world</SubItems></With></Else></Something></MyOtherObject>";   
        var otherObject =(MyOtherObject) new XmlSerializer(typeof(MyOtherObject)).Deserialize(new StringReader(otherObjectXmlString));
        Console.WriteLine(otherObject.SomethingString);             
    }
}

我创建了this fiddle,你可以试试看。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-01-04
    • 2015-01-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多