使用元数据反序列化 Xml

发布于 2024-12-25 13:19:45 字数 722 浏览 3 评论 0原文

我想将这个 Xml : 反序列化

<Content id="1">
  <Element key="Description">Bla  bla bla</Element>
  <Element key="Title">The title</Element>  
</Content>

到这个类中:

public class Content
{
[XmlAttribute(AttributeName = "id")]
        public string Id
        {
            get { return _id; }
            set { _id = value; }
        }
[XmlAttribute(AttributeName = "description")]
        public string Description
        {
            get;
            set;
        }
[XmlAttribute(XmlElement = "title")]
        public string Title
        {
            get;
            set;
        }
}

我的问题是我不知道如何将正确属性的文本放入类属性中。

谢谢

I want to deserialize this Xml :

<Content id="1">
  <Element key="Description">Bla  bla bla</Element>
  <Element key="Title">The title</Element>  
</Content>

into this classes :

public class Content
{
[XmlAttribute(AttributeName = "id")]
        public string Id
        {
            get { return _id; }
            set { _id = value; }
        }
[XmlAttribute(AttributeName = "description")]
        public string Description
        {
            get;
            set;
        }
[XmlAttribute(XmlElement = "title")]
        public string Title
        {
            get;
            set;
        }
}

My problem is that i don't know how i can put the text of the right attribute in the class property.

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

小女人ら 2025-01-01 13:19:46

您需要为 XML 节点创建一个辅助类。

另一种选择是实现 IXmlSerialized接口:

public class Content: IXmlSerializable
{
    public void WriteXml (XmlWriter writer)
    {
        // write element nodes
    }

    public void ReadXml (XmlReader reader)
    {
        // read element nodes
    }

    public XmlSchema GetSchema()
    {
        return null;
    }
}

You need to create a helper class for the <element> XML node.

Another option would be to implement the IXmlSerializable interface:

public class Content: IXmlSerializable
{
    public void WriteXml (XmlWriter writer)
    {
        // write element nodes
    }

    public void ReadXml (XmlReader reader)
    {
        // read element nodes
    }

    public XmlSchema GetSchema()
    {
        return null;
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文