1

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(SaveOptions.DisableFormatting);
}
R. Martinho Fernandes
  • 228,013
  • 71
  • 433
  • 510
SmartestVEGA
  • 8,415
  • 26
  • 86
  • 139

2 Answers2

7

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

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
  • i put the above code at the end but there is no xml outfile created. – SmartestVEGA Nov 03 '09 at 05:57
  • i tried this there is no files there! doc.Save(@"D:\VSProjects\SearchEE\SearchEE\bin\Debug\searchresults.xml"); – SmartestVEGA Nov 03 '09 at 06:02
  • i am not getting any error ; i do have access to that directory – SmartestVEGA Nov 03 '09 at 06:04
  • This project is at the same location: D:\VSProjects\SearchEE\SearchEE\bin\Debug – SmartestVEGA Nov 03 '09 at 06:06
  • is there any reference need to create in project ? – SmartestVEGA Nov 03 '09 at 06:07
  • This is last two lines of the code string xmlString = doc.ToString(SaveOptions.DisableFormatting); doc.Save(@"D:\VSProjects\SearchEE\SearchEE\bin\Debug\searchresults.xml"); – SmartestVEGA Nov 03 '09 at 06:07
  • OK, this is odd - this just works ....... can you check in the debugger that the string "xmlString" contains the correct XML as you'd expect? – marc_s Nov 03 '09 at 06:09
  • As for reference: since you're using Linq-to-XML, you need `using System.Xml.Linq;` but if you didn't have that, the code wouldn't even compile.... – marc_s Nov 03 '09 at 06:09
  • there is using System.Xml.Linq;, but there is no output in the string – SmartestVEGA Nov 03 '09 at 06:13
  • does the above code works for you, do u did anything extra other than copy pasting the code in the window? – SmartestVEGA Nov 03 '09 at 06:14
  • only copy&paste into a brand-new Winforms app - no tweaking of any kind. – marc_s Nov 03 '09 at 06:29
  • This is really really strange, so let's try crazy stuff... Is the Load event hooked up to the `Form1_Load` method you showed? Look it up in the Properties window, click in the little lightning bolt and scroll down to Load. Or, add a button, and place that code into the Click event handler. Run an click the button. Does it work? – R. Martinho Fernandes Nov 03 '09 at 06:29
  • can you try copy&pasting that code into a new winforms app, and also try a different path (e.g. C:\ or something). This is extremely weird...... – marc_s Nov 03 '09 at 06:29
3

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...

R. Martinho Fernandes
  • 228,013
  • 71
  • 433
  • 510
  • i put the above code at the end but there is no xml outfile created. Like this........... doc.ToString(SaveOptions.DisableFormatting); doc.Save(@"searchresults.xml"); – SmartestVEGA Nov 03 '09 at 05:58