问题描述
 
<?xml version =" 1.0"编码= QUOT; UTF-8英寸?>
$
<招标>
< Ravina>
&NBSP; < BillNo> 1< / BillNo>
&NBSP; < PageNo> 10< / PageNo>
&NBSP; <活动>金属< /活动>
< / Ravina>
< Ravina>
&NBSP; < BillNo> 2< / BillNo>
&NBSP; < PageNo> 20< / PageNo>
&NBSP; <活动> Formworks< / Activity>
< / Ravina>
< Ravina>
&NBSP; &NBSP; &NBSP; < BillNo> 3< / BillNo>
< PageNo> 30< / PageNo>
<活动> SiteWorks< / Activity>
< / Ravina>
< /招标>
var path = Server.MapPath(@"〜/ Product.xml");
&NBSP; &NBSP; &NBSP; XmlTextReader xmlreader = new XmlTextReader(path);
&NBSP; &NBSP; &NBSP; //读取xml数据
&NBSP; &NBSP; &NBSP; DataSet ds = new DataSet();
&NBSP; &NBSP; &NBSP; ds.ReadXml(xmlreader);
&NBSP; &NBSP; &NBSP; xmlreader.Close();
XmlDataDocument xmldoc = new XmlDataDocument();
&NBSP; &NBSP; &NBSP; XmlNodeList xmlnode;
&NBSP; &NBSP; &NBSP; int i = 0;
&NBSP; &NBSP; &NBSP; string str = null;
&NBSP; &NBSP; &NBSP; FileStream fs = new FileStream(path,FileMode.Open,FileAccess.Read);
&NBSP; &NBSP; &NBSP; xmldoc.Load(fs);
&NBSP; &NBSP; &NBSP; xmlnode = xmldoc.GetElementsByTagName(" Ravina");
&NBSP; &NBSP; &NBSP; for(i = 0; i< = xmlnode.Count - 1; i ++)
&NBSP; &NBSP; &NBSP; {
&NBSP; &NBSP; &NBSP; &NBSP; &NBSP; str = xmlnode [i] .ChildNodes.Item(0).InnerText.Trim();
&NBSP; &NBSP; &NBSP; &NBSP; &NBSP; str = xmlnode [i] .ChildNodes.Item(1).InnerText.Trim();
&NBSP; &NBSP; &NBSP; &NBSP; &NBSP; // str = xmlnode [i] .ChildNodes.Item(0).InnerText.Trim()+" &NBSP;" + xmlnode [i] .ChildNodes.Item(1).InnerText.Trim()+" &NBSP;" + xmlnode [i] .ChildNodes.Item(2).InnerText.Trim();
&NBSP; &NBSP; &NBSP; &NBSP; &NBSP; // MessageBox.Show(str);
&NBSP; &NBSP; &NBSP;使用(XmlWriter writer) b
= XmlWriter.Create(" C:\\\\
y \\ myXmFile.xml")))
&NBSP; &NBSP; &NBSP; {
&NBSP; &NBSP; &NBSP; &NBSP; &NBSP; writer.WriteStartDocument();
&NBSP; &NBSP; &NBSP; &NBSP; &NBSP; writer.WriteStartElement(" Employees");
&NBSP; &NBSP; &NBSP; &NBSP; &NBSP; // foreach(员工中的员工员工)
&NBSP; &NBSP; &NBSP; &NBSP; &NBSP; // {
&NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; writer.WriteStartElement(" Employee");
&NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; writer.WriteElementString(QUOT; ID"," 123"的ToString()); &NBSP; //< - 这些是新的$
&NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; writer.WriteElementString(" FirstName"," mohit");
&NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; writer.WriteElementString(" LastName"," tiwari");
&NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; writer.WriteElementString(" Salary"," 1500");
$
&NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; writer.WriteEndElement();
&NBSP; &NBSP; &NBSP; &NBSP; //}
&NBSP; &NBSP; &NBSP; &NBSP; &NBSP; writer.WriteEndElement();
&NBSP; &NBSP; &NBSP; &NBSP; &NBSP; writer.WriteEndDocument();
&NBSP; &NBSP; &NBSP; }
<?xml version="1.0" encoding="utf-8" ?>
<Tenders>
<Ravina>
<BillNo>1</BillNo>
<PageNo>10</PageNo>
<Activity>Metals</Activity>
</Ravina>
<Ravina>
<BillNo>2</BillNo>
<PageNo>20</PageNo>
<Activity>Formworks</Activity>
</Ravina>
<Ravina>
<BillNo>3</BillNo>
<PageNo>30</PageNo>
<Activity>SiteWorks</Activity>
</Ravina>
</Tenders>
var path = Server.MapPath(@"~/Product.xml");
XmlTextReader xmlreader = new XmlTextReader(path);
//reading the xml data
DataSet ds = new DataSet();
ds.ReadXml(xmlreader);
xmlreader.Close();
XmlDataDocument xmldoc = new XmlDataDocument();
XmlNodeList xmlnode;
int i = 0;
string str = null;
FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read);
xmldoc.Load(fs);
xmlnode = xmldoc.GetElementsByTagName("Ravina");
for (i = 0; i <= xmlnode.Count - 1; i++)
{
str= xmlnode[i].ChildNodes.Item(0).InnerText.Trim();
str = xmlnode[i].ChildNodes.Item(1).InnerText.Trim();
// str = xmlnode[i].ChildNodes.Item(0).InnerText.Trim() + " " + xmlnode[i].ChildNodes.Item(1).InnerText.Trim() + " " + xmlnode[i].ChildNodes.Item(2).InnerText.Trim();
// MessageBox.Show(str);
}
write xml
using (XmlWriter writer = XmlWriter.Create("C:\\ny\\myXmFile.xml")){
writer.WriteStartDocument();
writer.WriteStartElement("Employees");
//foreach (Employee employee in employees)
//{
writer.WriteStartElement("Employee");
writer.WriteElementString("ID","123".ToString()); // <-- These are new
writer.WriteElementString("FirstName", "mohit");
writer.WriteElementString("LastName", "tiwari");
writer.WriteElementString("Salary", "1500");
writer.WriteEndElement();
// }
writer.WriteEndElement();
writer.WriteEndDocument();
}
这篇关于读取xML不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!