質問

どのように私はC#のコードを次のXML出力を見ることができますか?私はそれがのXElementを使用していることを見ることができますが、どこでXMLファイルまたは出力を見つけることができますか?

private void Form1_Load(object sender, EventArgs e)
{
    XElement doc = new XElement("searchresults"); // root element

    //create 
    XElement result = new XElement("result",
                             new XElement("Resulthead", "AltaVista"),
                             new XElement("ResultURL", "www.altavista.com/"),
                             new XElement("Description", "AltaVista provides the most comprehensive search experience on the Web! ... "),
                             new XElement("DisplayURL", "www.altavista.com/")
                             );
    doc.Add(result);

    //add another search result
    result = new XElement("result",
                             new XElement("Resulthead", "Dogpile Web Search"),
                             new XElement("ResultURL", "www.dogpile.com/"),
                             new XElement("Description", "All the best search engines piled into one. All the best search engines piled into one."),
                             new XElement("DisplayURL", "www.dogpile.com/")
                             );

    doc.Add(result);

    string xmlString = doc.ToString(SaveOptions.DisableFormatting);
}
役に立ちましたか?

解決

あなたの結果は、あなたの「xmlString」変数内に存在する - それは、コンソール/ウィンドウの上に、またファイルにもない、どこにも書かれていない

あなたが追加する必要があります。

doc.Save(@"C:\your-xml-file-name.xml");

あなたの方法の最後の行は、ディスク上のファイルに内容を保存します。

の完全なパスを使用してください、またはアプリは(ほとんどの場合、すなわち(yourappname)\bin\debugに)実行されている現在のディレクトリにチェックします。

マルク

他のヒント

このコードはどこにもXMLが、メモリ(xmlString変数を)書いていません。

あなたは XElement.Save() を呼び出してみて、ファイルにそれを得ることができ:

doc.Save(@"filename.xml");

やデバッガを使用して、変数を見てみます。

それとも、あなたが好む場合は、単にテキストボックスに入れます:

textBox.Text = xmlString;

それはきれいにフォーマットされないことが警告され...

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top