3

通常の方法では、XMLファイルを作成するためのコードは次のとおりです。

    XmlWriterSettings settings = new XmlWriterSettings();
    settings.Indent = true;
    XmlWriter writer = XmlWriter.Create("Products.xml", settings);

    writer.WriteStartDocument();

    writer.WriteComment("This file is generated by the program.");

    writer.WriteStartElement("Product");
    writer.WriteAttributeString("ID", "001");
    writer.WriteAttributeString("Name", "Keyboard");
    writer.WriteElementString("Price", "10.00");
    writer.WriteStartElement("OtherDetails");
    writer.WriteElementString("BrandName", "X Keyboard");
    writer.WriteElementString("Manufacturer", "X Company");
    writer.WriteEndElement();
    writer.WriteEndDocument();
    writer.Flush();
    writer.Close();

しかし、上記のコードは私に異なるXML構造を与えます、私が以下の与えられた構造として出力を必要とする場合にコーディングする方法、

<Books>
<Book ISBN="0553212419">
<title>Sherlock Holmes</title>
<author>Sir Arthur Conan Doyle</author>
</Book>
<Book ISBN="0743273567">
<title>The Great Gatsby</title>
<author>F. Scott Fitzgerald</author>
</Book>
<Book ISBN="0684826976">
<title>Undaunted Courage</title>
<author>Stephen E. Ambrose</author>
</Book>
<Book ISBN="0743203178">
<title>Nothing Like It In the World</title>
<author>Stephen E. Ambrose</author>
</Book>
</Books>

ありがとう

4

2 に答える 2

15

コメントされたように、あなたがすでに正しい要素を書かなければならないコードを修正するだけです。

XmlWriter writer = XmlWriter.Create(@"Products.xml", settings);

writer.WriteStartDocument();

writer.WriteComment("This file is generated by the program.");

writer.WriteStartElement("Books");
writer.WriteStartElement("Book");
writer.WriteAttributeString("ISBN", "0553212419");
writer.WriteElementString("Title", "Sherlock Holmes");
writer.WriteElementString("Author", "Sir Arthur Conan Doyle");
writer.WriteEndElement();
writer.WriteEndElement();
writer.WriteEndDocument();
writer.Flush();
writer.Close();

洗って、すすぎ、繰り返します。それぞれの本を追加する方法を書くことをお勧めします。

編集-本を書くための方法

void WriteBookData(XmlWriter writer, string isbn, string title, string author)
{
    writer.WriteStartElement("Book");
    writer.WriteAttributeString("ISBN", isbn);
    writer.WriteElementString("Title", title);
    writer.WriteElementString("Author", author);
    writer.WriteEndElement();
}

XmlWriterSettings settings = new XmlWriterSettings();
settings.Indent = true;
using (XmlWriter writer = XmlWriter.Create(@"Products.xml", settings))
{
    writer.WriteStartDocument();

    writer.WriteComment("This file is generated by the program.");

    writer.WriteStartElement("Books");
    WriteBookData(writer, "0553212419", "Sherlock Holmes", "Sir Arthur Conan Doyle");
    writer.WriteEndElement();
    writer.WriteEndDocument();
    writer.Flush();
}
于 2012-05-30T03:54:03.340 に答える
3

Linq to XML を使用してみませんか。非常に簡単で、理解するのが非常に簡単です。

   string filename = string.Empty;
 XElement res= new XElement ("Books",new XElement ("Book",new XAttribute   ("ISBN","="+055321212),
            new XElement ("Title","Sherlock Homes"),
            new XElement ("author","Sir Arthur Conan")
            )
            );


        res.Save(filename);

foreach look を使用して、上記のステートメントにハードコードされた値を格納できます

于 2012-05-30T04:01:26.693 に答える