我有2个数据表fireCollarTable_和electrofusionTable_。
fireCollarTable_ = new System.Data.DataTable("Fire Collar");
fireCollarTable_.Columns.Add("Article", typeof(string));
fireCollarTable_.Columns.Add("Description", typeof(string));
fireCollarTable_.Columns.Add("Diameter", typeof(double));
electroFusionCouplerTable_ = new System.Data.DataTable("Electrofusion Coupler");
electroFusionCouplerTable_.Columns.Add("Article", typeof(string));
electroFusionCouplerTable_.Columns.Add("Description", typeof(string));
electroFusionCouplerTable_.Columns.Add("Diameter", typeof(double));
electroFusionCouplerTable_.Columns.Add("Length", typeof(double));
定义了上述两个数据表,XML文件是
<Catalogue>
<!-- 1-Item, 2-Article, 3-Description, 4-PipeType, 5-Diameter -->
<Record a1="Fire Collar" a2="VS0410001" a3="Fire Collar, 40 mm" a5="40" />
<Record a1="Fire Collar" a2="VS0410003" a3="Fire Collar, 50 mm" a5="50" />
<Record a1="Fire Collar" a2="VS0410005" a3="Fire Collar, 56 mm" a5="56" />
<!-- 1-Item, 2-Article, 3-Description, 4-PipeType, 5-Diameter,9-Length-->
<Record a1="Electrofusion Coupler" a2="VS0350001" a3="Electrofusion coupler, 40 mm" a5="40" a9="64" />
<Record a1="Electrofusion Coupler" a2="VS0350003" a3="Electrofusion coupler, 50 mm" a5="50" a9="60" />
</Catalogue>
我想解析XML文件,并根据属性“a1”的值,我想将数据插入到一个数据表中。例如如果“a1”=“Fire Collar”的值,则应将数据插入firecollar表中。
答案 0 :(得分:1)
您应该使用XPath在C#中解析XML文件。请点击此链接“http://support.microsoft.com/kb/308333”和“http://www.codeproject.com/Articles/52079/Using-XPathNavigator-in-C#”。 我在这里给你一个例子:
您可以使用参数搜索特定节点,并在需要放置XmlNode.Value时放置它。
在Google上搜索此内容,您将获得准确的语法和流程。
答案 1 :(得分:0)
XmlDocument xmlDocument = new XmlDocument();
xmlDocument.Load(@"\XMLFile1.xml");
XmlNodeList fireCollarNodeList = xmlDocument.SelectNodes("/Catalogue/Record[@a1='Fire Collar']");
foreach (XmlNode fireCollarNode in fireCollarNodeList)
{
// add details to the fireCollarTable_
}
XmlNodeList electroFusionCouplerNodeList = xmlDocument.SelectNodes("/Catalogue/Record[@a1='Electrofusion Coupler']");
foreach (XmlNode fireCollarNode in fireCollarNodeList)
{
// add details to the electroFusionCouplerTable_
}