كيف يمكنني كتابة إخراج XML؟
سؤال
كيف يمكنني رؤية إخراج XML من رمز C # التالي؟ أستطيع أن أرى أنه يستخدم 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;
أن تحذير قد لا يتم تنسيقها بشكل جيد ...
لا تنتمي إلى StackOverflow