簡體   English   中英

使用LINQ to XML將XML文件插入現有XML文件

[英]Insert XML file into existing XML file using LINQ to XML

使用在SO上找到的一些示例代碼 ,我整理了一些代碼來打開現有的XML數據庫,打開另一個要插入第一個XML的XML文件,獲取其中的根節點的后代。文件,並將它們附加到database.xml文件中所選節點的基礎上,然后將結果文件保存到新的XML文件中。 下面的代碼可以在VS2010中編譯並運行,沒有錯誤,但沒有將XML從actionID.xml文件添加到database.xml文件,我也不知道缺少什么。

這是C#代碼:

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

class pcbdbUpdate
{
static void Main( )
    {
        //open database & load into memory
        XDocument PCBDB = XDocument.Load("C:\\database.xml");
        //open Activity Log file & load into memory
        XDocument addAction = XDocument.Load("C:\\actionID.xml");
        //get descendant node(s) below PCBDBActivityLog root node
        var actionXML = addAction.Root.Descendants("PCBDBActivityLog");
        /*supposed to write out child nodes, but not very useful, just tells me
        it's a:  System.Xml.Linq.XContainer+<GetDescendants>d__a*/
        Console.WriteLine(actionXML.ToString());
        //enumerate database for SBE_PCB_Data nodes
        IEnumerable<XElement> SBE_PCB_Data = PCBDB.Element("PCBDatabase").Elements("SBE_PCB_Data");
        //look for specific node with PCBID of "00001" and select it
        XElement newAction = SBE_PCB_Data.Where(p => p.Attribute("PCBID").Value == "00001").FirstOrDefault();
        //write descendants from Activity Log to base of node
        newAction.Add(actionXML);
        //save the resultant file
        PCBDB.Save("C:\\newDatabase.xml");
    }
}

這是database.xml:

<?xml version="1.0" encoding="utf-8"?>
<PCBDatabase>
  <SBE_PCB_Data PCBID="00001">
    <Creation ActionID="0002" User="DELLIOTTG:192.168.1.69" Date="2012-10-31T14:35:58" PCBID="00001">
      <PCBDrawing>00001a</PCBDrawing>
      <AssemblyDrawing>00001b</AssemblyDrawing>
      <Vendor>SBE</Vendor>
      <PONumber>00000</PONumber>
    </Creation>
    <Assignment ActionID="1295" User="RHO:192.168.1.6" Date="2012-12-13T08:59:31" PCBID="00001">
      <PCBDrawing>00002a</PCBDrawing>
      <AssemblyDrawing>00001c</AssemblyDrawing>
      <Vendor>SBE</Vendor>
      <PONumber>00001</PONumber>
    </Assignment>   
  </SBE_PCB_Data>
  <SBE_PCB_Data PCBID="00002">
    <Assignment ActionID="630c" User="DMUELLER:192.168.1.152" Date="2010-03-15T13:14:21" PCBID="00002">
      <SBEJobNumber>57380</SBEJobNumber>
    </Assignment>
  </SBE_PCB_Data>
</PCBDatabase>

這是actionID.xml:

<?xml version="1.0"?>
<PCBDBActivityLog>
    <Assignment ActionID='8353' User='DMUELLER:192.168.1.134' Date='2011-01-27T15:38:25' PCBID='00001'>
        <SBEPN>41528E</SBEPN>
    </Assignment>
</PCBDBActivityLog>

結果文件應如下所示:

<?xml version="1.0" encoding="utf-8"?>
<PCBDatabase>
  <SBE_PCB_Data PCBID="00001">
    <Creation ActionID="0002" User="DELLIOTTG:192.168.1.69" Date="2012-10-31T14:35:58" PCBID="00001">
      <PCBDrawing>00001a</PCBDrawing>
      <AssemblyDrawing>00001b</AssemblyDrawing>
      <Vendor>SBE</Vendor>
      <PONumber>00000</PONumber>
    </Creation>
    <Assignment ActionID="1295" User="RHO:192.168.1.6" Date="2012-12-13T08:59:31" PCBID="00001">
      <PCBDrawing>00002a</PCBDrawing>
      <AssemblyDrawing>00001c</AssemblyDrawing>
      <Vendor>SBE</Vendor>
      <PONumber>00001</PONumber>
    </Assignment>
    <Assignment ActionID='8353' User='DMUELLER:192.168.1.134' Date='2011-01-27T15:38:25' PCBID='00001'>
        <SBEPN>41528E</SBEPN>
    </Assignment>
  </SBE_PCB_Data>
  <SBE_PCB_Data PCBID="00002">
    <Assignment ActionID="630c" User="DMUELLER:192.168.1.152" Date="2010-03-15T13:14:21" PCBID="00002">
      <SBEJobNumber>57380</SBEJobNumber>
    </Assignment>
  </SBE_PCB_Data>
</PCBDatabase>

我想念什么或做錯什么?

錯誤在這里:

var actionXML = addAction.Root.Descendants("PCBDBActivityLog")` 

您試圖在文檔根目錄下找到<PCBDBActivityLog>元素。 但這不會返回任何內容,因為<PCBDBActivityLog>元素 actionID.xml文檔的根。 將此行替換為

var actionXML = addAction.Descendants("PCBDBActivityLog");

要么

var actionXML = addAction.Root;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM