[Serializable]
public class Thing
{
[XmlAttribute]
public string Name {get;set;}
[XmlAnyElement]
public XmlNode Document { get; set; }
}
这将有一个不幸的副作用,因为您将发现不仅仅是subnode1和subnode2(所有
空白
也要反序列化。。。呃),所以你需要过滤掉它们:
class Program
{
static void Main()
{
const string xml = @"
<Thing Name=""George"">
<Document>
<subnode1/>
<subnode2/>
</Document>
</Thing>";
var s = new XmlSerializer(typeof(Thing));
var thing = s.Deserialize(new StringReader(xml)) as Thing;
foreach (XmlNode node in thing.Document)
{
// should filter to only subnode1 and subnode2.
if (node.Name != "" && node.Name != "#whitespace")
{
Console.WriteLine(node.Name);
}
}
Console.ReadLine();
}
}
希望这有帮助!