使用c#asp.net获取XML数据并插入sql server
现在我正在尝试做一些服务,在那里我可以读取xml文件并将其解析为sql server,我已经阅读并看到许多教程如何使用c#将xml解析为sql server,但我仍然无法获取数据使用c#asp.net获取XML数据并插入sql server,c#,asp.net,sql-server,xml,C#,Asp.net,Sql Server,Xml,现在我正在尝试做一些服务,在那里我可以读取xml文件并将其解析为sql server,我已经阅读并看到许多教程如何使用c#将xml解析为sql server,但我仍然无法获取数据 <?xml version="1.0" encoding="utf-8" ?> <Source 1 - Subject 17> SubjectType: Faces FaceConfidence: 100 <appeared at 02/08/2018 5:28:4
<?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>
主题类型:面
信心指数:100
框架索引:1033
矩形:在(210;169)处,宽度=63,高度=84
框架索引:1050
矩形:在(210;134)处,宽度=70,高度=94
框架索引:1050
状态:Ok
眼睛在:(260;169)和(229;169)
矩形:在(210;134)处,宽度=70,高度=94
时间戳:2018年8月2日下午5:28:46
没有找到匹配项
包含成功生成的模板
这是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();
}
受保护的无效按钮1\u单击(对象发送者,事件参数e)
{
字符串cs=@“数据源=172.16.6.39;初始目录=FC_扫描;持久安全信息=True;用户ID=FC_adm;密码=P@ssw0rd";
SqlConnection con=新的SqlConnection(cs);
XmlDocument doc=新的XmlDocument();
doc.Load(“test.xml”);
var source=doc.DocumentElement.SelectNodes(“source”).Cast().ToList();
var似乎=源[0]。GetAttribute(“似乎”);
var失踪=源[0]。GetAttribute(“失踪”);
var top=source[0]。GetAttribute(“top”);
SqlCommand命令cmd;
SqlDataAdapter da=新的SqlDataAdapter();
字符串sql=null;
con.Open();
sql=“插入到源值中(“+Source+”、“+executed+”、“+designed+”、“+top+”)”;
cmd=新的SqlCommand(sql,con);
da.InsertCommand=cmd;
da.InsertCommand.ExecuteNonQuery();
con.Close();
}
错误控制台总是出现在xml文件中,任何帮助、技巧或提示我都会非常感激。当我启动代码时,我立即在
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.
显然,它不喜欢名为“source1-Subject 17”、“出现在2018年8月2日下午5:28:43”等XML标记
更新:
您不能使用.Net XML操作工具,因为您的文件不是XML(它声称是带有头的XML,但有点像谎言)。如果仍然需要将此文件的内容放入数据库,则必须编写自定义解析例程来读取该自定义数据格式,或者,如果可能,联系为您生成此“XML”的人员,说服他们将其数据格式更改为真实的XML。如果您在同一个组织中工作等,这可能是最简单的方法。当我启动您的代码时,我立即在
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.
显然,它不喜欢名为“source1-Subject 17”、“出现在2018年8月2日下午5:28:43”等XML标记
更新:
您不能使用.Net XML操作工具,因为您的文件不是XML(它声称是带有头的XML,但有点像谎言)。如果仍然需要将此文件的内容放入数据库,则必须编写自定义解析例程来读取该自定义数据格式,或者,如果可能,联系为您生成此“XML”的人员,说服他们将其数据格式更改为真实的XML。如果您在同一个组织工作等,这可能是最简单的方法。您的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>
面孔
100
2018年8月2日下午5:28:43>
1033
2018年8月2日下午5:28:44>
1050
1050
好啊
2018年8月2日下午5:28:46
没有找到匹配项
您的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>
面孔
100
2018年8月2日下午5:28:43>
1033
2018年8月2日下午5:28:44>
1050
1050
好啊
2018年8月2日下午5:28:46
没有找到匹配项
我对jdweng的回答不是百分之百的满意,但他的想法告诉我,实际上应该有多个根元素。
所以我认为每个来源都有自己的来源和主题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>
面孔
100
2018年8月2日下午5:28:43
1033
2018年8月2日下午5:28:44
1050
1050
好啊
2018年8月2日下午5:28:46
没有找到匹配项
面孔
101
2018年8月2日下午6:28:43
1034
2018年8月2日下午6:28:44
1051
1051
好啊
2018年8月2日下午6:28:46
没有找到匹配项
我对jdweng的回答不是百分之百的满意,但他的想法告诉我,实际上应该有多个根元素。
所以我认为每个来源都有自己的来源和主题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>
面孔
100
2018年8月2日下午5:28:43
1033
2018年8月2日下午5:28:44
1050
1050
好啊
2018年8月2日下午5:28:46
没有找到匹配项
面孔
101
2018年8月2日下午6:28:43
1034
2018年8月2日下午6:28:44
1051
1051
好啊
2018年8月2日下午6:28:46
没有找到匹配项
查看。。。这将使事情变得更简单。SQL表是什么样子的?我将处理xml错误。应该很快会有答案。谢谢filburt,我会尝试一下,对于表格式,我只是使用nvarchar和字段源,出现、消失和顶部..你从哪里得到这个xml?它的格式很糟糕?@DwiRahmanIsmail你无法控制这个其他应用程序,因为如果你这样做,你应该输出有效的xml?看看。。。这将使事情变得更简单。SQL表是什么样子的?我将处理xml错误。应该很快就会有答案。谢谢filburt,我会尝试一下,对于表格式,我只是使用nvarchar和字段源,出现,di