C# クラスを作成しました。
public class books {
public int bookNum { get; set; }
public class book {
public string name { get; set; }
public class record {
public string borrowDate { get; set; }
public string returnDate { get; set; }
}
public record[] records { get; set; }
}
public book[] books { get; set; }
}
しかし、XmlSerializer を使用して XML 文字列に変換するときです。結果は以下の xml と同じではありません。
私の C# クラスの問題は何ですか? XmlDocument を使用する代わりに、XmlSerializer を使用して結果を出力したいと考えています。
何か案は?前もって感謝します!
<books>
<bookNum>2</bookNum>
<book>
<name>Book 1</name>
<record>
<borrowDate>2013-7-1</borrowDate>
<returnDate>2013-7-12</returnDate>
</record>
<record>
<borrowDate>2013-8-1</borrowDate>
<returnDate>2013-8-5</returnDate>
</record>
</book>
<book>
<name>Book 2</name>
<record>
<borrowDate>2013-6-1</borrowDate>
<returnDate>2013-6-12</returnDate>
</record>
<record>
<borrowDate>2013-7-1</borrowDate>
<returnDate>2013-7-5</returnDate>
</record>
</book>
</books>
編集
以下は私のC#コードと出力結果です:
books books = new books {
bookNum = 2,
Books = new books.book[] {
new books.book {
name = "Book1",
records = new books.book.record[] {
new books.book.record {
borrowDate = "2013-1-3",
returnDate = "2013-1-5"
},
new books.book.record {
borrowDate = "2013-2-3",
returnDate = "2013-4-5"
}
}
},
new books.book {
name = "Book1",
records = new books.book.record[] {
new books.book.record {
borrowDate = "2013-1-3",
returnDate = "2013-1-5"
},
new books.book.record {
borrowDate = "2013-2-3",
returnDate = "2013-4-5"
}
}
}
}
};
XmlSerializer xsSubmit = new XmlSerializer(typeof(books));
XmlDocument doc = new XmlDocument();
System.IO.StringWriter sww = new System.IO.StringWriter();
XmlWriter writer = XmlWriter.Create(sww);
xsSubmit.Serialize(writer, books);
var xml = sww.ToString(); // Your xml
context.Response.Write(xml);
XML:
<books>
<bookNum>2</bookNum>
<Books>
<book>
<name>Book1</name>
<records>
<record>
<borrowDate>2013-1-3</borrowDate>
<returnDate>2013-1-5</returnDate>
</record>
<record>
<borrowDate>2013-2-3</borrowDate>
<returnDate>2013-4-5</returnDate>
</record>
</records>
</book>
<book>
<name>Book1</name>
<records>
<record>
<borrowDate>2013-1-3</borrowDate>
<returnDate>2013-1-5</returnDate>
</record>
<record>
<borrowDate>2013-2-3</borrowDate>
<returnDate>2013-4-5</returnDate>
</record>
</records>
</book>
</Books>
</books>