根据C#中的id在现有xml中插入新的子节点

时间:2016-09-26 09:36:39

标签: c# xml

我想在现有的xml中添加子节点



        <tblTemp>
          <Details>
            <LoginId>4</LoginId>
            <AId>2</AId>
            <OId>763</OId>
            <LDate>2016-09-26</LDate>
            <LTime>15:27:39</LTime>
            <ReasonId>1</ReasonId>
            <Flag>2</Flag>
           </Details>
          <Details>
            <LoginId>3</LoginId>
            <AId>2</AId>
            <OId>763</OId>
            <LDate>2016-09-26</LDate>
            <LTime>12:22:39</LTime>
            <ReasonId>4</ReasonId>
            <Flag>2</Flag>
           </Details>
           <Details>
            <LoginId>1</LoginId>
            <AId>1</AId>
            <OId>765</OId>
            <LDate>2016-09-26</LDate>
            <LTime>10:22:39</LTime>
            <ReasonId>4</ReasonId>
            <Flag>2</Flag>
           </Details>
         </tblTemp>
&#13;
&#13;
&#13;

我想要这样的输出

&#13;
&#13;
    <tblTemp>
      <Details>
        <LoginId>4</LoginId>
        <AId>2</AId>
        <OId>763</OId>
        <LDate>2016-09-26</LDate>
        <LTime>15:27:39</LTime>
         <FDate>2016-09-26</FDate>
        <FTime>16:50:30</FTime>
        <ReasonId>1</ReasonId>
        <Flag>2</Flag>
       </Details>
      <Details>
        <LoginId>3</LoginId>
        <AId>2</AId>
        <OId>763</OId>
        <LDate>2016-09-26</LDate>
        <LTime>12:22:39</LTime>
         <FDate>2016-09-26</FDate>
        <FTime>13:36:30</FTime>
        <ReasonId>4</ReasonId>
        <Flag>2</Flag>
       </Details>
       <Details>
        <LoginId>1</LoginId>
        <AId>1</AId>
        <OId>765</OId>
        <LDate>2016-09-26</LDate>
        <LTime>10:22:39</LTime>
        <FDate>2016-09-26</FDate>
        <FTime>11:53:45</FTime>
        <ReasonId>4</ReasonId>
        <Flag>2</Flag>
       </Details>
     </tblTemp>
&#13;
&#13;
&#13;

基于LoginId我想在xml文件中添加子节点。我一直在尝试这样的代码。

//code for adding child node
 string strDBDir = "C:\\XMLfile.xml";
                     try
                     {
                         DataSet dsxml = new DataSet();
                         DataView DvXML = null;
                         dsxml.ReadXml(strDBDir);
                         DvXML = dsxml.Tables[0].DefaultView;
                         DvXML.RowFilter = "AId = '" + AId + "'";

                         if (File.Exists(strDBDir))
                         {
                             if (DvXML.ToTable().Rows.Count > 0)
                             {
                                 LoginId = Convert.ToInt32(DvXML.ToTable().Rows[0]["LoginId"]);

                             XmlDocument originalXml = new XmlDocument();
                             originalXml.Load(strDBDir);
                             XmlNode TechReport = originalXml.SelectSingleNode("Details");
                             XmlNode Data = originalXml.CreateNode(XmlNodeType.Element, "FDate", null);
                             TechReport.AppendChild(Data);
                             originalXml.Save(strDBDir);
                         }
                     }
                     catch
                     {
                     }

对于上面的代码,我得到一个异常 - &#34;对象引用未设置为对象的实例&#34; 你能否指导一下,如何在DBfile.xml中添加基于LoginId和AId的FDate和Ftime。我一直都在努力。

4 个答案:

答案 0 :(得分:0)

您可以通过更改搜索来实现这一目标。 而不只是寻找&#34;细节&#34;将随机添加子元素,使用&#34;详细信息[LoginId =&#39; 4&#39;]&#34;

XmlNode TechReport = originalXml.SelectSingleNode("Details[LoginId='4']");

这是你在找什么?

另外,我认为你需要使用CreateElement而不是CreateNode

答案 1 :(得分:0)

使用xml linq并更改所有出现次数

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;

namespace ConsoleApplication16
{
    class Program
    {
        const string FILENAME = @"c:\temp\test.xml";
        static void Main(string[] args)
        {
            XDocument doc = XDocument.Load(FILENAME);

            foreach (XElement detail in doc.Descendants("Details"))
            {
                detail.Add(new object[] {
                    new XElement("LTime", DateTime.Now.ToString("hh:mm:ss")),
                    new XElement("FDate", DateTime.Now.Date.ToShortDateString())
                });
            }

        }
    }
}

答案 2 :(得分:0)

我认为

XmlNode TechReport = originalXml.SelectSingleNode("Details");

应该是:

XmlNode TechReport = originalXml.SelectSingleNode("tblTemp/Details");

或者更确切地说:

XmlDocument originalXml = new XmlDocument();
originalXml.Load(strDBDir);
var TechReport = originalXml.SelectSingleNode($"tblTemp/Details[AId={AId}][LoginId={LoginId}]");
if (TechReport != null)
{
  XmlNode Data = originalXml.CreateNode(XmlNodeType.Element, "FDate", null);
  TechReport.AppendChild(Data);
  originalXml.Save(strDBDir);
}
else
{
  // Handle this as you see fit...
}

答案 3 :(得分:0)

您可以尝试这种方式添加新的孩子:

 XmlDocument xml = new XmlDocument();
 xml.Load(strDBDir);
 XmlNodeList xnList = xml.SelectNodes("/tblTemp/Details");
 foreach (XmlNode xn in xnList)
 {
    // here your **if** statement with check on loginId in **xn**
    XElement child = new XElement("FDate"); 
    child.SetValue("2016-09-26");
    xn.AppendChild(child);
 }