我的XML文件结构如下所示:

<SalaryDetails>
    <Employee>
        <Name>George Dsouza</Name>
        <AnnualSalary>320000</AnnualSalary>
        <DaysWorked>22</DaysWorked>
    </Employee>
    <Employee>
        <Name>Jackie Parera</Name>
        <AnnualSalary>300000</AnnualSalary>
        <DaysWorked>19</DaysWorked>
    </Employee>
...
</SalaryDetails>

我想使用XmlDocument将所有数据作为受雇者记录放入数据库中。

所以我写了这样的循环:
XmlDocument xdcDocument = new XmlDocument();

xdcDocument.Load(@"D:\SalaryDetails.xml");

XmlElement xelRoot = xdcDocument.DocumentElement;
XmlNodeList xnlNodes = xelRoot.SelectNodes("/SalaryDetails/Employee");

foreach(XmlNode xndNode in xnlNodes)
    {
        //What to write here??
        //My sql insert command will go here
    }
AnnualSalaryDaysWorked是整数。

最佳答案

尝试:

foreach (XmlNode xndNode in xnlNodes)
{
  string name= xndNode ["Name"].InnerText;
  string AnnualSalary= xndNode ["AnnualSalary"].InnerText;
  string DaysWorked= xndNode ["DaysWorked"].InnerText;

 //Your sql insert command will go here;
}

关于c# - 遍历XML文档,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/14806661/

10-13 08:57