(我不确切知道我的问题是什么,所以我希望我用正确的标签和标题问它) 当我获取我的xml文件并让for-loop启动时,它会执行此操作
1(错误前的x断点)(本地人)
name "0" string
enetSportCode "1.0" string
x del "2011-03-15 13:53:12" string
n 0 int
ut null string
sportid 0 int
之后我得到了错误 “输入字符串的格式不正确” 所以我知道n得到的值不是int 但如果我想把它变成一个字符串就会发生
2(错误前的x断点)(本地人)
name "0" string
enetSportCode "1.0" string
del "2011-03-15 13:53:12" string
x n "0.171" string
ut null string
sportid 0 int
这次错误 “指数数组的边界之外” 我不知道为什么会这样做,但它本应该这样做
name "Soccer" string
enetSportCode "s" string
del "no" string
n "1" int
ut "2009-12-29 15:36:24" string
sportid "1" int
这是我使用的代码 C#部分
private void OpenFileButton_Click(object sender, EventArgs e)
{
Stream xmlStream = null;
OpenFileDialog openFile1 = new OpenFileDialog();
openFile1.InitialDirectory = "C:\\";
openFile1.Filter = "xml files (*.xml)|*.xml|All files (*.*)|*.*";
openFile1.FilterIndex = 1;
openFile1.RestoreDirectory = true;
if (openFile1.ShowDialog() == DialogResult.OK)
{
try
{
if ((xmlStream = openFile1.OpenFile()) != null)
{
using (xmlStream)
{
SqlConnection myConnection = new SqlConnection("Database=;Server=;Integrated Security=True;connect timeout = 30");
SqlCommand command;
SqlDataAdapter adpter = new SqlDataAdapter();
DataSet ds = new DataSet();
XmlReader xmlFile ;
string sql;
string name;
string enetSportCode;
string del;
int n;
string ut;
int sportid;
xmlFile = XmlReader.Create(openFile1.FileName, new XmlReaderSettings());
ds.ReadXml(xmlFile);
int i = 0;
int j = 0;
myConnection.Open();
for (i = 0; i <= ds.Tables[j].Rows.Count; i++)
{
name = ds.Tables[j].Rows[i].ItemArray[0].ToString();
enetSportCode = ds.Tables[j].Rows[i].ItemArray[1].ToString();
del = ds.Tables[j].Rows[i].ItemArray[2].ToString();
n = Convert.ToInt32(ds.Tables[j].Rows[i].ItemArray[3]);
ut = ds.Tables[j].Rows[i].ItemArray[4].ToString();
sportid = Convert.ToInt32(ds.Tables[j].Rows[i].ItemArray[5]);
sql = "insert into sports('" + name + "','" + enetSportCode + "','" + del + "'," + n + ",'" + ut + "'," + sportid + ")";
command = new SqlCommand(sql, myConnection);
adpter.InsertCommand = command;
adpter.InsertCommand.ExecuteNonQuery();
}
myConnection.Close();
MessageBox.Show("complete");
}
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
}
}
xml part
<?xml version="1.0" encoding="UTF-8"?>
<spocosy version="1.0" responsetime="2011-03-15 13:53:12" exec="0.171">
<query-response requestid="" service="objectquery">
<sport>
<sport name="Soccer" enetSportCode="s" del="no" n="1" ut="2009-12-29 15:36:24" id="1"/>
</sport>
答案 0 :(得分:0)
在继续之前,我对你的xml结构进行了一些小修改: 为spocosy和查询响应添加结束标记
<?xml version="1.0" encoding="UTF-8"?>
<spocosy version="1.0" responsetime="2011-03-15 13:53:12" exec="0.171">
<query-response requestid="" service="objectquery">
<sport>
<sport name="Soccer" enetSportCode="s" del="no" n="1" ut="2009-12-29 15:36:24" id="1"/>
</sport>
</query-response>
</spocosy>
首先,您需要确保所有属性都可用。您收到'索引超出数组范围'错误的事实告诉我们缺少一个或多个属性。当发生这种情况时,你有5个或更少的属性,所以当要求'ItemArray [5]'时会默认引发错误
如何抓住这个?使您的xml属性为Obligated或使用属性名称而不是数组值。这意味着在处理任何内容之前更改代码或进行验证。
ie:ds.Rows [j] .ItemArray.Count&lt; 6 =&gt;抛出:缺少属性
如何捕获0.172?变化
sportid = Convert.ToInt32(ds.Tables[j].Rows[i].ItemArray[5])
到
sportid = Int.TryParse(ds.Tables[j].Rows[i].ItemArray[5], out sportid) ? sportid : 0;
LINQ中的旧答案
然后:让我们使用Linq:
//Load xml
XDocument xdoc = XDocument.Load("data.xml");
//Run query
var sports = from sport in xdoc.Descendants("spocosy")
.Descendants("query-response")
.Descendants("sport")
.Descendants("sport")
select new {
Name= sport.Attribute("name").Value,
EnetSportCode = sport.Attribute("enetSportCode").Value,
Del = sport.Attribute("del").Value,
N = Convert.ToInt32(sport.Attribute("n").Value),
UT = sport.Attribute("ut").Value,
SportId = Convert.ToInt32(sport.Attribute("id").Value)
};
var sqlLines = new List<string>();
//Loop through results
foreach (var sport in sports)
{
sqlLines.Add(string.Format("Insert into sports(NameColumn, EnetSportCodeColumn, DelColumn, nColumn, utColumn, sportidColumn) VALUES('{0}', '{1}', '{2}', {3}, '{4}', {5})",
sport.Name,
sport.EnetSportCode,
sport.Del,
sport.N,
sport.UT,
sport.SportId));
}
另外:插入的正确语法是:
Insert into TableName
(ColumnA, ColumnB, ColumnC)
VALUES
(ValueA, ValueB, ValueC)
不会强>
Insert Into TableName
(ValueA, ValueB, ValueC)
编辑:另一个注释:在基于INT的列中插入DOUBLE / DECIMAL / FLOAT值0.172将不起作用。更重要的是,它会在Convert.ToInt32()...
行产生错误EDIT2:你如何在xml中收到一个NULL值?是属性abscent /它只是“”?