XML 출력을 어떻게 작성할 수 있습니까?
문제
다음 C# 코드의 XML 출력을 어떻게 볼 수 있습니까? xlement를 사용하는 것을 볼 수 있지만 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;
잘 지내지 않을 수도 있습니다 ...
제휴하지 않습니다 StackOverflow