for(int j=1;j<14;j++)
{
if (myXmlReader.Name == "a" + j.ToString())
{
cm.Parameters.AddWithValue("@A" + j.ToString(),myXmlReader.ReadElementString("a" + j.ToString()));
} }写成这种的总是出错
把cm放到if外层就可以,但是这样就不能取到xml的结点,不知道这是为什么?
因为有几十个参数,手工添加参数实在是太多了。
{
if (myXmlReader.Name == "a" + j.ToString())
{
cm.Parameters.AddWithValue("@A" + j.ToString(),myXmlReader.ReadElementString("a" + j.ToString()));
} }写成这种的总是出错
把cm放到if外层就可以,但是这样就不能取到xml的结点,不知道这是为什么?
因为有几十个参数,手工添加参数实在是太多了。
我有另一条语句:TextArea1.InnerText += myXmlReader.ReadElementString();
在TextArea1中可以看到读出的结果PS:csdn现在怎么总是出错
不用这么判断,去掉。ReadElementString会自动移动至下一条之前
---------------------------
那要是自动移动的话我就很难确定是哪个是我要的结点了
--------------------------------------------
哎,我看也只能这样了,这个XmlReader把我折磨了半个月了,简单点的xml文件还应付的来,复杂结构的文件真是力不从心顺便问个问题:我这个Xml文件中有几十个结点(而且结点的结构比较复杂),大文件估计要上百,XmlDocument能使用循环方式来读取再存入数据库吗?
XmlDocument当然可以用循环的方式读取了。
XmlDocument doc = new XmlDocument();
doc.Load("items.xml");
XmlElement root = doc.DocumentElement;
Console.WriteLine(root.ChildNodes.Count);
//直接定位
XmlNodeList els = root.GetElementsByTagName("item3");
Console.WriteLine(els[0].InnerText);
//循环读取1
XmlNode node = root.FirstChild; while (node != null)
{
Console.WriteLine("{0}---{1}", node.Name, node.InnerText);
node = node.NextSibling;
} foreach (XmlNode nod in root.ChildNodes)
{
Console.WriteLine("{0}---{1}", nod.Name, nod.InnerText);
}