问题描述
运行此代码时出现xml错误.我正在尝试制作一个xml文件.但是我一直收到运行时错误.
I get a xml error when i run this code. I am trying to make a xml file. However I keep getting a run time error.
私有 无效 SetupXMLfile()
private void SetupXMLfile()
{
int 行数= 0;
int rowcount = 0;
int 已找到;
int found;
int 单元= 1;
int cell = 1;
ArrayList elementarray = new ArrayList ();
ArrayList elementarray = new ArrayList();
//mydoc.Load(filename);
//mydoc.Load(filename);
XmlTextWriter xw = 新 XmlTextWriter (文件名,System.Text. 编码 .UTF8);
XmlTextWriter xw = new XmlTextWriter(filename, System.Text.Encoding.UTF8);
xw.WriteStartDocument();
xw.WriteStartDocument();
xw.WriteStartElement(RootNodestring);
xw.WriteStartElement(RootNodestring);
foreach ( DataGridViewRow 字体> 行 XMLelement_dataGridView.Rows)
foreach (DataGridViewRow row in XMLelement_dataGridView.Rows)
{
if (XMLelement_dataGridView.Rows [rowcount] .Cells [cell] .Value!= null )
if (XMLelement_dataGridView.Rows[rowcount].Cells[cell].Value != null)
{
elementarray.Sort();
elementarray.Sort();
found = elementarray.BinarySearch(XMLelement_dataGridView.Rows [rowcount] .Cells [cell] .Value);
found = elementarray.BinarySearch(XMLelement_dataGridView.Rows[rowcount].Cells[cell].Value);
如果 (发现< 0)
if (found < 0)
{ //如果找到则开始
{//start of if for found
//elementarray.Add(XMLelement_dataGridView.Rows [rowcount] .Cells [cell] .Value);
// elementarray.Add(XMLelement_dataGridView.Rows[rowcount].Cells[cell].Value);
//xw.WriteStartElement(XMLelement_dataGridView.Rows [rowcount] .Cells [cell] .Value.ToString(),XMLelement_dataGridView.Rows [rowcount].单元格[cell + 1] .Value.ToString());
//xw.WriteStartElement(XMLelement_dataGridView.Rows[rowcount].Cells[cell].Value.ToString(), XMLelement_dataGridView.Rows[rowcount].Cells[cell + 1].Value.ToString());
xw.WriteAttributeString(XMLelement_dataGridView.Rows [rowcount] .Cells [cell] .Value.ToString(),XMLelement_dataGridView.Rows [rowcount] .Cells [cell + 1] .Value.ToString());
xw.WriteAttributeString(XMLelement_dataGridView.Rows[rowcount].Cells[cell].Value.ToString(), XMLelement_dataGridView.Rows[rowcount].Cells[cell + 1].Value.ToString());
//ChildElement.SetAttribute(XMLelement_dataGridView.Rows [rowcount] .Cells [cell] .Value.ToString(),XMLelement_dataGridView.Rows [rowcount].单元格[cell + 1] .Value.ToString());
//ChildElement.SetAttribute(XMLelement_dataGridView.Rows[rowcount].Cells[cell].Value.ToString(), XMLelement_dataGridView.Rows[rowcount].Cells[cell + 1].Value.ToString());
行数++;
}
其他 //继续下一个值
else //continue with the next vaule
{
行数++;
继续 ;
continue;
}
} //if的结尾
}//end of if
} //每个的结尾
}//end of for each
xw.WriteEndElement();
xw.WriteEndElement();
xw.WriteEndAttribute();
xw.WriteEndAttribute();
xw.WriteEndDocument();
xw.WriteEndDocument();
xw.Flush();
xw.Flush();
xw.Close();
xw.Close();
}
推荐答案
您的代码中没有start属性,如果WriteAttributeString方法没有关闭该属性,那么它们将是错误的方法
There is no start attribute in your code, and if the WriteAttributeString method was not closing the attribute, they would be the wrong way around
xw.WriteEndElement();
xw.WriteEndElement();
您的XML仅包含一个元素,您也可以编写一个平面文件.如果您尝试用相同的名称编写两个属性,则也会出错,因为您要在一个元素中写入大量的属性.
Your XML has only one element in it, you may as well write a flat file. You'll also get an error if you try to write two attributes with the same name, as you're writing a ton of attributes in to the one element.
很难知道您注释掉的代码是按什么顺序尝试的,但是我看不到任何组合都能奏效.供以后参考,确切的异常和抛出该异常的行将对尝试帮助您的人们有所帮助.
It's hard to know what code that you have commented out you have tried in what order, but no combination I can see will work. For future reference, the exact exception and hte line that throws it would help people trying to help you.
这篇关于XML ERRO的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!