我需要在c#中读取一个xml文档并将数据放到记事本中 [英] I need to read a xml document in c# and drop the data to notepad
本文介绍了我需要在c#中读取一个xml文档并将数据放到记事本中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要在c#中读取一个xml文档并将数据放到记事本中
这是XML文件(我不想写标记给NOTEPAD,我只是信息)
请帮忙!
<访客>< br />
<访客>< br />
< FirstName> Test1< / FirstName>< br />
< LastName> Test12< / LastName>< br />
< ContactNo> 1234567890< / ContactNo> ;< br />
<电子邮件> test@test.com< /电子邮件>< br />
<日期> 7/17/2013 9:04:48 AM< / Date> ;< br />
< /访客>< br />
< /访客>
解决方案
嗨afshandc - 这应该是诀窍:
string onlyContent = string .Empty;
XmlDocument xdoc = new XmlDocument();
xdoc.Load(pathToXml);
var visitor = xdoc.SelectNodes( 访客/访问者跨度>);
for ( int i = 0 ; i < visitor.Count; i ++)
{
// 标题新条目
onlyContent + = string .Format( ---- VISITOR {0} ------ \\\\ n,i);
// 读取所有节点innertext
foreach (XmlNode节点在 visitor [i] .ChildNodes)
onlyContent + = string .Format( {0} \\ n ,node.InnerText);
}
File.WriteAllText(yourOutputfile,onlyContent);
private static DataSet ReadXMLFile( string filePath)
{
try
{
DataSet ds = new DataSet ();
if (!string.IsNullOrEmpty(filePath))
{
ds.ReadXml(filePath);
}
return ds;
}
catch (例外)
{
抛跨度>;
}
}
数据将保存在数据集中,知道你可以像你一样写数据想。
帮助链接写入档案
写入文本文件
TextWriter
尝试使用此链接阅读XML。使用StreamWriter将输出写入文本文件。
http:// support.microsoft.com/kb/307548 [ ^ ]
I need to read a xml document in c# and drop the data to notepad
This is the XML File(I DONT WANT TO WRITE THE TAGS TO NOTEPAD,I ONLY THE INFO)
Please Help!
<Visitors><br /> <Visitor><br /> <FirstName>Test1</FirstName><br /> <LastName>Test12</LastName><br /> <ContactNo>1234567890</ContactNo><br /> <Email>test@test.com</Email><br /> <Date>7/17/2013 9:04:48 AM</Date><br /> </Visitor><br /> </Visitors>
解决方案
Hi afshandc - this should to the trick:
string onlyContent = string.Empty; XmlDocument xdoc = new XmlDocument(); xdoc.Load(pathToXml); var visitors = xdoc.SelectNodes("Visitors/Visitor"); for (int i = 0; i < visitors.Count; i++) { //headlining the new entry onlyContent += string.Format("----VISITOR {0}------\r\n",i); //read all nodes innertext foreach (XmlNode node in visitors[i].ChildNodes) onlyContent += string.Format("{0}\r\n", node.InnerText); } File.WriteAllText(yourOutputfile, onlyContent);
private static DataSet ReadXMLFile(string filePath) { try { DataSet ds = new DataSet(); if (!string.IsNullOrEmpty(filePath)) { ds.ReadXml(filePath); } return ds; } catch (Exception) { throw; } }
data will be save in dataset know you can write data as you want.
helping links for writing on file
Write to a Text File
TextWriter
Try this link for reading the XML. Use StreamWriter to write the output to Text File.
http://support.microsoft.com/kb/307548[^]
这篇关于我需要在c#中读取一个xml文档并将数据放到记事本中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文