开发者

How can I write XML output?

开发者 https://www.devze.com 2022-12-10 06:08 出处:网络
How can I see the XML output of following C# code? I can see that it uses XElement, but where I can locate the XML file or the output?

How can I see the XML output of following C# code? I can see that it uses XElement, but where I can locate the XML file or the output?

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(SaveOpti开发者_开发百科ons.DisableFormatting);
}


Your result only exists inside your "xmlString" variable - it's not being written anywhere, neither onto the console / window, nor into a file.

You'll have to add a

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

line at the end of your method to save the contents to a file on disk.

Make sure to use a full path, or check in your current directory where the app is running (i.e. in (yourappname)\bin\debug, most likely).

Marc


That code is not writing XML anywhere but memory (the xmlString variable).

You could try calling XElement.Save() and get it on a file:

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

Or use the debugger and look at the variables.

Or, if you prefer, simply put it in a TextBox:

textBox.Text = xmlString;

Be warned it may not be nicely formatted...

0

精彩评论

暂无评论...
验证码 换一张
取 消