题
如何查看下面的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:
textBox.Text = xmlString;
被警告它可能无法很好地格式化......
不隶属于 StackOverflow