简体   繁体   English

从标记为可序列化的对象获取XMlNode内容

[英]Get the XMlNode content from object marked as serializable

I have a class Image implementing ISerializable : 我有一个实现ISerializable的类Image

[Serializable]
[XmlRoot(ElementName = "IMAGE")]
[TypeConverter(typeof(ImageTypeConverter))]
public class ImageResource : ISerializable {
    [XmlAttribute(AttributeName = "TYPE")]
    public string Extension{
        get;
        set;
    }
}

I just want to know if we can get the xml node for an object of this class? 我只想知道我们是否可以为此类的对象获取xml节点? Suppose this object is serializes as 假设此对象序列化为

<IMAGE TYPE=".mpg"/>

I want to get this Node content as string. 我想以字符串形式获取此Node内容。

The XML representation does not exist until you serialize the instance. 序列化实例之前,XML表示形式不存在。 Once you serialize it, you can manipulate it as XML. 序列化后,可以将其作为XML进行操作。

Hint: you should be able to serialize directly into an XDocument : 提示:您应该能够直接序列化为XDocument

XDocument doc = new XDocument();
using (XmlWriter writer = doc.CreateNavigator().AppendChild())
{
    XmlSerializer ser = new XmlSerializer(typeof(ImageResource));
    ser.Serialize(instance);
}

没关系,请从下面的链接文本中获得答案

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM