簡體   English   中英

XML序列化問題 - 如何從一個對象序列化元素,屬性和文本

[英]XML Serialization question - How to Serialize Element, Attribute and Text from One Object

我是使用.NET進行XML序列化的新手,在使用它一段時間之后我現在非常費力。 我可以使用包含其他元素的屬性來序列化元素,但是如何序列化類似的元素

<myElement name="foo">bar</myElement>

我為myElement使用了一個帶有XmlAttribute的“name”類,但是如何引用XML Element的值?

提前致謝。

[XmlText] ,如下:

using System;
using System.Xml.Serialization;
[Serializable, XmlRoot("myElement")]
public class MyType {
    [XmlAttribute("name")]
    public string Name {get;set;}

    [XmlText]
    public string Text {get;set;}
} 
static class Program {
    static void Main() {
        new XmlSerializer(typeof(MyType)).Serialize(Console.Out,
            new MyType { Name = "foo", Text = "bar" });
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM