0

次の XML ファイルがあります。

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<a>
    <b attr0="">
        <c>
            <d attr1="" attr2="">
                <e>
                    <f/>
                    <g/>
                    <h/>
                    <i/>
                </e>
            </d>
                    <!-- ...more d's -->
        </c>
    </b>
    <b>
        <c>
            <d attr1="" attr2="">
                <e>
                    <f/>
                    <g/>
                    <h/>
                    <i/>
                </e>
            </d>
                    <!-- ...more d's -->
        </c>        
    </b>
    <!-- ...more b's -->
</a>

それを C# オブジェクトに逆シリアル化したいのですが、次のクラスを使用しています。

クラスa:

[XmlRoot(ElementName = "a")]
public class a
{
    [XmlElement("b")]
    List<b> bs = new List<b>();
}

クラスb:

public class b
{
    [XmlAttribute("attr0")]
    String attr0{ get; set; }
    [XmlElement("c")]
    c c1 = new c();
}

クラス c:

public class c
{
    [XmlElement("d")]
    List<d> ds = new List<d>();
}

クラスd:

public class d
{
    [XmlAttribute(AttributeName = "attr1")]
    String attr1{ get; set; }
    [XmlAttribute(AttributeName = "attr2")]
    String attr2{ get; set; }
    [XmlElement("e")]
    List<e> es = new List<e>(); 
}

およびクラス e:

public class e
{
    [XmlText]
    String f { get; set; }
    [XmlText]
    String g { get; set; }
    [XmlText]
    String h { get; set; }
    [XmlText]
    String i { get; set; }
}

そして、次のコードで私はそれを逆シリアル化しようとします:

    public a deserialize()
    {
        XmlSerializer deserializer = new XmlSerializer(typeof(a));
        System.IO.TextReader reader = new System.IO.StreamReader(@"C:\file.xml");
        object obj = deserializer.Deserialize(reader);
        a XmlData = (a)obj;
        reader.Close();
        return a;
    }

さて、今のところ、何も機能していません。XMLArray タグを追加しようとしましたが、うまくいきませんでした。あなたたちは私に良いアドバイスをしてくれると思います:)

4

2 に答える 2

0
XElement xmlFile= new XElement("a",
                                new XElement("b",
                                    new XElement("c",
                                        new XElement("d", new XAttribute("attr1"," ") , new XAttribute("attr2"," ")),
                                        new XElement("e", new XElement("f"), new XElement("g"), new XElement("h"), new XElement("i")),
                                    new XElement("c",
                                         new XElement("d", new XAttribute("attr1"," ") , new XAttribute("attr2"," ")),
                                         new XElement("e", new XElement("f"), new XElement("g"), new XElement("h"), new XElement("i")),
                                    new XElement("Department", new XAttribute("Name","Automobile"))
                                ),
                                new XElement("b",
                                    new XElement("c",
                                        new XElement("d", new XAttribute("attr1"," ") , new XAttribute("attr2"," ")),
                                        new XElement("e", new XElement("f"), new XElement("g"), new XElement("h"), new XElement("i")),
                                    new XElement("c",
                                         new XElement("d", new XAttribute("attr1"," ") , new XAttribute("attr2"," ")),
                                         new XElement("e", new XElement("f"), new XElement("g"), new XElement("h"), new XElement("i"))
                                )

); xmlFile.Save(@"D:\file.xml");

問題の解決に役立つ場合は、これを試してください。そして、私がシリアル化したのと同じ方法で逆シリアル化できます。

編集:

コードの最後の行も追加してみてください。コードセクションの一部を追加しようとしましたが、どういうわけか起こりません。

于 2013-10-23T08:34:16.443 に答える