c# - IXmlSerializable, reading xml tree with many nested elements -
Can you give me an example to write and write XML in this way:
< Pre> & lt; Foolist & gt; & Lt; Foo name = "A" & gt; & Lt; Hair name = "hair 1" /> & Lt; Hair name = "hair 2" /> & Lt; / Foo & gt; & Lt; Foo name = "B" /> & Lt; Foo name = "C" & gt; & Lt; Hair name = "hair 1" & gt; & Lt; Husband's name = "small1" /> & Lt; / Child & gt; & Lt; / Foo & gt; & Lt; Foolist & gt;
Does the element name actually change per level? If not, you can use a very simple class model and XmlSerializer
. The implementation is IXmlSerializable
... difficult; And avoid the error prone, do not use it until you use it at all.
If the names are different but are rigid, then I just run it through XSD:
xsd example.xml xsd example IXmlSerializable
for example (Same name at each level): usingXmlSerializer
system for
.xsd / classes
; Using System.Collections.Generic; Using System.Xml.Serialization; [XmlRoot ("stupid")] Public class record {public record (name of string): it () {name = name; } Public records () {children = new list & lt; Record & gt; (); } [XmlAttribute ("name")] public string name {get; Set; } [XmlElement ("hair")] public list & lt; Record & gt; Children {receive; Set; }} Static class program {static zero main () {record root = new record {children = {new record ("a") {children = {new record ("hair 1"), new record ("hair 2"), }}, New record ("b"), new record ("c") {children = {new record ("hair 1") {children = {new record ("little 1")}}}}}; Var ser = new XmlSerializer (typef (record)); Serialize (console.out, root); }}
Comments
Post a Comment