现在,我正在尝试提供一些服务,以便我可以读取xml文件并将其解析为sql服务器,我已经阅读并看到了许多教程如何使用c#将xml解析为sql服务器,但仍然无法使用数据。
<?xml version="1.0" encoding="utf-8" ?>
<Source 1 - Subject 17>
SubjectType: Faces
FaceConfidence: 100
<appeared at 02/08/2018 5:28:43 PM>
FrameIndex: 1033
Rectangle: at (210;169), width=63, height=84
</appeared at 02/08/2018 5:28:43 PM>
<track at 02/08/2018 5:28:44 PM>
FrameIndex: 1050
Rectangle: at (210;134), width=70, height=94
<Details available on frame 1050>
FrameIndex: 1050
Status: Ok
Eyes at: (260; 169) and (229; 169)
Rectangle: at (210;134), width=70, height=94
</Details available on frame 1050>
</track at 02/08/2018 5:28:44 PM>
<disappeared at 02/08/2018 5:28:46 PM>
TimeStamp: 02/08/2018 5:28:46 PM
<Top 1000 of Best Matches>
no matches found
</Top 1000 of Best Matches>
Contains successfully generated template
</disappeared at 02/08/2018 5:28:46 PM>
</Source 1 - Subject 17>
这是xml文件格式,这是我的尝试:
protected void Button1_Click(object sender, EventArgs e)
{
string cs = @"Data Source=172.16.6.39;Initial Catalog=FC_SCAN;Persist Security Info=True;User ID=fc_adm;Password=P@ssw0rd";
SqlConnection con = new SqlConnection(cs);
XmlDocument doc = new XmlDocument();
doc.Load("test.xml");
var source = doc.DocumentElement.SelectNodes("Source").Cast<XmlElement>().ToList();
var appeared = source[0].GetAttribute("Appeared");
var disappeared = source[0].GetAttribute("Disappeared");
var top = source[0].GetAttribute("Top");
SqlCommand cmd;
SqlDataAdapter da = new SqlDataAdapter();
string sql = null;
con.Open();
sql = "Insert into Source values ('" + source + "','" + appeared + "','" + disappeared + "','"+top+"')";
cmd = new SqlCommand(sql, con);
da.InsertCommand = cmd;
da.InsertCommand.ExecuteNonQuery();
con.Close();
}
错误控制台始终显示在xml文件中,我非常感谢任何帮助,技巧或提示。
答案 0 :(得分:2)
启动代码时,我立即在处收到异常
doc.Load("test.xml");
行。异常消息很清楚:
An unhandled exception of type 'System.Xml.XmlException' occurred in System.Xml.dll
Additional information: Name cannot begin with the '1' character, hexadecimal value 0x31. Line 2, position 9.
显然,它不喜欢名为“源1-主题17”,“出现在02/08/2018 5:28:43 PM”等的XML标签。
更新:
您不能使用.Net XML操纵的东西,因为您的文件不是XML(它声称是带有标头的XML,但是有点儿谎言)。如果仍然需要将此文件的内容放入数据库,则必须编写一个自定义解析例程以读取该自定义数据格式,或者,如果可能的话,请与为您生成“ XML”的人员联系并说服他们将其数据格式更改为真实的XML。如果您在同一组织等中工作,这可能是最简单的方法。
答案 1 :(得分:1)
您的xml太糟糕了。我将其固定在下面:
<?xml version="1.0" encoding="utf-8" ?>
<Source>
<SubjectType>Faces</SubjectType>
<FaceConfidence>100</FaceConfidence>
<appeared>
02/08/2018 5:28:43 PM>
<FrameIndex>1033</FrameIndex>
<Rectangle top="210" left="169" width="63" height="84"/>
</appeared>
<track>
02/08/2018 5:28:44 PM>
<FrameIndex>1050</FrameIndex>
<Rectangle top="210" left="134" width="70" height="94"/>
<Details>
<FrameIndex>1050</FrameIndex>
<Status>Ok</Status>
<Eyes>
<location x="260" y="169"/>
<location x="229" y="169"/>
</Eyes>
<Rectangle top="210" left="134" width="70" height="94"/>
</Details>
</track>
<disappeared>
<TimeStamp>02/08/2018 5:28:46 PM</TimeStamp>
<Top_1000> no matches found</Top_1000>
</disappeared>
</Source>
答案 2 :(得分:1)
我对 jdweng 的回答不是100%,但是他的想法告诉我实际上应该有多个根元素。 所以我的想法是,每个来源都有自己的来源和主题ID。
<?xml version="1.0" encoding="utf-8" ?>
<Source>
<Source id="1">
<Subject id="17">
<SubjectType>Faces</SubjectType>
<FaceConfidence>100</FaceConfidence>
<appeared>
02/08/2018 5:28:43 PM
<FrameIndex>1033</FrameIndex>
<Rectangle top="210" left="169" width="63" height="84"/>
</appeared>
<track>
02/08/2018 5:28:44 PM
<FrameIndex>1050</FrameIndex>
<Rectangle top="210" left="134" width="70" height="94"/>
<Details>
<FrameIndex>1050</FrameIndex>
<Status>Ok</Status>
<Eyes>
<location x="260" y="169"/>
<location x="229" y="169"/>
</Eyes>
<Rectangle top="210" left="134" width="70" height="94"/>
</Details>
</track>
<disappeared>
<TimeStamp>02/08/2018 5:28:46 PM</TimeStamp>
<Top_1000> no matches found</Top_1000>
</disappeared>
</Subject>
</Source>
<Source id="2">
<Subject id="18">
<SubjectType>Faces</SubjectType>
<FaceConfidence>101</FaceConfidence>
<appeared>
02/08/2018 6:28:43 PM
<FrameIndex>1034</FrameIndex>
<Rectangle top="210" left="169" width="63" height="84"/>
</appeared>
<track>
02/08/2018 6:28:44 PM
<FrameIndex>1051</FrameIndex>
<Rectangle top="210" left="134" width="70" height="94"/>
<Details>
<FrameIndex>1051</FrameIndex>
<Status>Ok</Status>
<Eyes>
<location x="260" y="169"/>
<location x="229" y="169"/>
</Eyes>
<Rectangle top="210" left="134" width="70" height="94"/>
</Details>
</track>
<disappeared>
<TimeStamp>02/08/2018 6:28:46 PM</TimeStamp>
<Top_1000> no matches found</Top_1000>
</disappeared>
</Subject>
</Source>
</Source>