2013-04-09 172 views
0

我是C#的新手。我試圖通過向其中的不同工作表添加一堆節點來編輯spreadsheetXML 2003文檔。這裏是一個結構的例子:用C編輯XML文檔#

<Workbook> 
<Worksheet ss:Name="Contact"> 
    <Table> 
    <Column /> 
    <Row /> 
    </Table> 
</Worksheet> 
<Worksheet ss:Name="Facility"> 
    <Table> 
    <Column /> 
    <Row /> 
    </Table> 
</Worksheet> 
</Workbook> 

我需要做的是加載這個XML並在某些地方插入新的節點。我很確定我知道如何創建新的XElement,並且需要它下面的節點(這裏有大量的在線教程)。我很難找到的是如何找到節點並在其下添加節點。這是據我已經有了:

XElement xmlDoc = XElement.Load(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "\\Autodesk\\Revit\\Addins\\2013\\COBiev2.4Template.xml"); 

所以,我怎麼找到「基金」工作表,並在其表節點下添加一個新行?我需要能夠這樣做,以至於在我的例程結束時,我可以將xmlDoc文件保存迴文件系統。

---- UPDATE ----- 我沒有得到這個工作。修改後的代碼和下面的實際XML的開始。

<?xml version="1.0"?> 
<?mso-application progid="Excel.Sheet"?> 
<Workbook xmlns="urn:schemas-microsoft-com:office:spreadsheet" 
xmlns:o="urn:schemas-microsoft-com:office:office" 
xmlns:x="urn:schemas-microsoft-com:office:excel" 
xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet" 
xmlns:html="http://www.w3.org/TR/REC-html40"> 
<Worksheet ss:Name="Contact"> 
    <Names> 
    <NamedRange ss:Name="_FilterDatabase" ss:RefersTo="=Contact!R1C1:R1C19" 
    ss:Hidden="1"/> 
    </Names> 
    <Table ss:ExpandedColumnCount="19" ss:ExpandedRowCount="1" x:FullColumns="1" 
    x:FullRows="1" ss:DefaultColumnWidth="49.5" ss:DefaultRowHeight="15"> 
    <Column ss:StyleID="s1247" ss:AutoFitWidth="0" ss:Width="209.25"/> 
    <Column ss:StyleID="s1251" ss:AutoFitWidth="0" ss:Width="209.25"/> 
    <Column ss:StyleID="s1247" ss:AutoFitWidth="0" ss:Width="160.5"/> 
    <Column ss:StyleID="s1251" ss:AutoFitWidth="0" ss:Width="119.25"/> 
    <Column ss:StyleID="s1247" ss:AutoFitWidth="0" ss:Width="129"/> 
    <Column ss:StyleID="s1247" ss:AutoFitWidth="0" ss:Width="82.5"/> 
    <Column ss:StyleID="s1252" ss:AutoFitWidth="0" ss:Width="165"/> 
    <Column ss:StyleID="s1252" ss:AutoFitWidth="0" ss:Width="129.75"/> 
    <Column ss:StyleID="s1252" ss:AutoFitWidth="0" ss:Width="45"/> 
    <Column ss:StyleID="s1248" ss:AutoFitWidth="0" ss:Width="45"/> 
    <Column ss:StyleID="s1248" ss:AutoFitWidth="0" ss:Width="129"/> 
    <Column ss:StyleID="s1248" ss:AutoFitWidth="0" ss:Width="66"/> 
    <Column ss:StyleID="s1248" ss:AutoFitWidth="0" ss:Width="107.25"/> 
    <Column ss:StyleID="s1248" ss:AutoFitWidth="0" ss:Width="198"/> 
    <Column ss:StyleID="s1248" ss:AutoFitWidth="0" ss:Width="65.25"/> 
    <Column ss:StyleID="s1248" ss:AutoFitWidth="0" ss:Width="72"/> 
    <Column ss:StyleID="s1248" ss:AutoFitWidth="0" ss:Width="45"/> 
    <Column ss:StyleID="s1253" ss:AutoFitWidth="0" ss:Width="62.25"/> 
    <Column ss:StyleID="s1248" ss:AutoFitWidth="0" ss:Width="45"/> 
    <Row ss:Height="93"> 
    <Cell ss:StyleID="s1254"><Data ss:Type="String">Email</Data><NamedCell 
     ss:Name="_xlnm._FilterDatabase_4"/><NamedCell ss:Name="_FilterDatabase"/><NamedCell 
     ss:Name="Contact.Name"/></Cell> 
    <Cell ss:StyleID="s1254"><Data ss:Type="String">CreatedBy</Data><NamedCell 
     ss:Name="_xlnm._FilterDatabase_4"/><NamedCell ss:Name="_FilterDatabase"/></Cell> 
    <Cell ss:StyleID="s1254"><Data ss:Type="String">CreatedOn</Data><NamedCell 
     ss:Name="_xlnm._FilterDatabase_4"/><NamedCell ss:Name="_FilterDatabase"/></Cell> 
    <Cell ss:StyleID="s1254"><Data ss:Type="String">Category</Data><NamedCell 
     ss:Name="_xlnm._FilterDatabase_4"/><NamedCell ss:Name="_FilterDatabase"/></Cell> 
    <Cell ss:StyleID="s1254"><Data ss:Type="String">Company</Data><NamedCell 
     ss:Name="_xlnm._FilterDatabase_4"/><NamedCell ss:Name="_FilterDatabase"/></Cell> 
    <Cell ss:StyleID="s1254"><Data ss:Type="String">Phone</Data><NamedCell 
     ss:Name="_xlnm._FilterDatabase_4"/><NamedCell ss:Name="_FilterDatabase"/></Cell> 
    <Cell ss:StyleID="s1254"><Data ss:Type="String">ExternalSystem</Data><NamedCell 
     ss:Name="_xlnm._FilterDatabase_4"/><NamedCell ss:Name="_FilterDatabase"/></Cell> 
    <Cell ss:StyleID="s1254"><Data ss:Type="String">ExternalObject</Data><NamedCell 
     ss:Name="_xlnm._FilterDatabase_4"/><NamedCell ss:Name="_FilterDatabase"/></Cell> 
    <Cell ss:StyleID="s1254"><Data ss:Type="String">ExternalIdentifier</Data><NamedCell 
     ss:Name="_xlnm._FilterDatabase_4"/><NamedCell ss:Name="_FilterDatabase"/></Cell> 
    <Cell ss:StyleID="s1254"><Data ss:Type="String">Department</Data><NamedCell 
     ss:Name="_xlnm._FilterDatabase_4"/><NamedCell ss:Name="_FilterDatabase"/></Cell> 
    <Cell ss:StyleID="s1254"><Data ss:Type="String">OrganizationCode</Data><NamedCell 
     ss:Name="_xlnm._FilterDatabase_4"/><NamedCell ss:Name="_FilterDatabase"/></Cell> 
    <Cell ss:StyleID="s1254"><Data ss:Type="String">GivenName</Data><NamedCell 
     ss:Name="_xlnm._FilterDatabase_4"/><NamedCell ss:Name="_FilterDatabase"/></Cell> 
    <Cell ss:StyleID="s1254"><Data ss:Type="String">FamilyName</Data><NamedCell 
     ss:Name="_xlnm._FilterDatabase_4"/><NamedCell ss:Name="_FilterDatabase"/></Cell> 
    <Cell ss:StyleID="s1254"><Data ss:Type="String">Street</Data><NamedCell 
     ss:Name="_xlnm._FilterDatabase_4"/><NamedCell ss:Name="_FilterDatabase"/></Cell> 
    <Cell ss:StyleID="s1254"><Data ss:Type="String">PostalBox</Data><NamedCell 
     ss:Name="_xlnm._FilterDatabase_4"/><NamedCell ss:Name="_FilterDatabase"/></Cell> 
    <Cell ss:StyleID="s1254"><Data ss:Type="String">Town</Data><NamedCell 
     ss:Name="_xlnm._FilterDatabase_4"/><NamedCell ss:Name="_FilterDatabase"/></Cell> 
    <Cell ss:StyleID="s1254"><Data ss:Type="String">StateRegion</Data><NamedCell 
     ss:Name="_xlnm._FilterDatabase_4"/><NamedCell ss:Name="_FilterDatabase"/></Cell> 
    <Cell ss:StyleID="s1255"><Data ss:Type="String">PostalCode</Data><NamedCell 
     ss:Name="_xlnm._FilterDatabase_4"/><NamedCell ss:Name="_FilterDatabase"/></Cell> 
    <Cell ss:StyleID="s1254"><Data ss:Type="String">Country</Data><NamedCell 
     ss:Name="_xlnm._FilterDatabase_4"/><NamedCell ss:Name="_FilterDatabase"/></Cell> 
    </Row> 
    </Table> 
    <WorksheetOptions xmlns="urn:schemas-microsoft-com:office:excel"> 
    <PageSetup> 
    <Header x:Margin="0.51180555555555551"/> 
    <Footer x:Margin="0.51180555555555551"/> 
    </PageSetup> 
    <Print> 
    <ValidPrinterInfo/> 
    <HorizontalResolution>300</HorizontalResolution> 
    <VerticalResolution>300</VerticalResolution> 
    </Print> 
    <TabColorIndex>43</TabColorIndex> 
    <Zoom>81</Zoom> 
    <FreezePanes/> 
    <FrozenNoSplit/> 
    <SplitHorizontal>1</SplitHorizontal> 
    <TopRowBottomPane>1</TopRowBottomPane> 
    <SplitVertical>1</SplitVertical> 
    <LeftColumnRightPane>1</LeftColumnRightPane> 
    <ActivePane>0</ActivePane> 
    <Panes> 
    <Pane> 
    <Number>3</Number> 
    </Pane> 
    <Pane> 
    <Number>1</Number> 
    </Pane> 
    <Pane> 
    <Number>2</Number> 
    </Pane> 
    <Pane> 
    <Number>0</Number> 
    <ActiveRow>0</ActiveRow> 
    <ActiveCol>0</ActiveCol> 
    </Pane> 
    </Panes> 
    <ProtectObjects>False</ProtectObjects> 
    <ProtectScenarios>False</ProtectScenarios> 
    <EnableSelection>NoSelection</EnableSelection> 
    </WorksheetOptions> 
    <AutoFilter x:Range="R1C1:R1C19" 
    xmlns="urn:schemas-microsoft-com:office:excel"> 
    </AutoFilter> 
</Worksheet> 
<Worksheet ss:Name="Facility"> 
    <Names> 
    <NamedRange ss:Name="_FilterDatabase" ss:RefersTo="=Facility!R1C1:R1C22" 
    ss:Hidden="1"/> 
    </Names> 
    <Table ss:ExpandedColumnCount="22" ss:ExpandedRowCount="1" x:FullColumns="1" 
    x:FullRows="1" ss:DefaultColumnWidth="49.5" ss:DefaultRowHeight="15"> 
    <Column ss:StyleID="s1247" ss:AutoFitWidth="0" ss:Width="46.5"/> 
    <Column ss:StyleID="s1251" ss:AutoFitWidth="0" ss:Width="174"/> 
    <Column ss:StyleID="s1247" ss:AutoFitWidth="0" ss:Width="162"/> 
    <Column ss:StyleID="s1251" ss:AutoFitWidth="0" ss:Width="96"/> 
    <Column ss:StyleID="s1247" ss:AutoFitWidth="0" ss:Width="116.25"/> 
    <Column ss:StyleID="s1247" ss:AutoFitWidth="0" ss:Width="45.75"/> 
    <Column ss:StyleID="s1251" ss:AutoFitWidth="0" ss:Width="45.75"/> 
    <Column ss:StyleID="s1251" ss:AutoFitWidth="0" ss:Width="78"/> 
    <Column ss:StyleID="s1251" ss:AutoFitWidth="0" ss:Width="69"/> 
    <Column ss:StyleID="s1251" ss:AutoFitWidth="0" ss:Width="45.75"/> 
    <Column ss:StyleID="s1247" ss:AutoFitWidth="0" ss:Width="230.25"/> 
    <Column ss:StyleID="s1252" ss:AutoFitWidth="0" ss:Width="179.25"/> 
    <Column ss:StyleID="s1252" ss:AutoFitWidth="0" ss:Width="54.75"/> 
    <Column ss:StyleID="s1252" ss:AutoFitWidth="0" ss:Width="156"/> 
    <Column ss:StyleID="s1252" ss:AutoFitWidth="0" ss:Width="45.75"/> 
    <Column ss:StyleID="s1252" ss:AutoFitWidth="0" ss:Width="154.5"/> 
    <Column ss:StyleID="s1252" ss:AutoFitWidth="0" ss:Width="60.75"/> 
    <Column ss:StyleID="s1252" ss:AutoFitWidth="0" ss:Width="156"/> 
    <Column ss:StyleID="s1248" ss:AutoFitWidth="0" ss:Width="116.25" ss:Span="1"/> 
    <Column ss:Index="21" ss:StyleID="s1248" ss:AutoFitWidth="0" ss:Width="45.75"/> 
    <Column ss:StyleID="s1248" ss:AutoFitWidth="0" ss:Width="74.25"/> 
    <Row ss:Height="124.5"> 
    <Cell ss:StyleID="s1254"><Data ss:Type="String">Name</Data><NamedCell 
     ss:Name="_xlnm._FilterDatabase_7"/><NamedCell ss:Name="_FilterDatabase"/><NamedCell 
     ss:Name="Facility.Name"/></Cell> 
    <Cell ss:StyleID="s1254"><Data ss:Type="String">CreatedBy</Data><NamedCell 
     ss:Name="_xlnm._FilterDatabase_7"/><NamedCell ss:Name="_FilterDatabase"/></Cell> 
    <Cell ss:StyleID="s1254"><Data ss:Type="String">CreatedOn</Data><NamedCell 
     ss:Name="_xlnm._FilterDatabase_7"/><NamedCell ss:Name="_FilterDatabase"/></Cell> 
    <Cell ss:StyleID="s1254"><Data ss:Type="String">Category</Data><NamedCell 
     ss:Name="_xlnm._FilterDatabase_7"/><NamedCell ss:Name="_FilterDatabase"/></Cell> 
    <Cell ss:StyleID="s1254"><Data ss:Type="String">ProjectName</Data><NamedCell 
     ss:Name="_xlnm._FilterDatabase_7"/><NamedCell ss:Name="_FilterDatabase"/></Cell> 
    <Cell ss:StyleID="s1254"><Data ss:Type="String">SiteName</Data><NamedCell 
     ss:Name="_xlnm._FilterDatabase_7"/><NamedCell ss:Name="_FilterDatabase"/></Cell> 
    <Cell ss:StyleID="s1254"><Data ss:Type="String">LinearUnits</Data><NamedCell 
     ss:Name="_xlnm._FilterDatabase_7"/><NamedCell ss:Name="_FilterDatabase"/></Cell> 
    <Cell ss:StyleID="s1254"><Data ss:Type="String">AreaUnits</Data><NamedCell 
     ss:Name="_xlnm._FilterDatabase_7"/><NamedCell ss:Name="_FilterDatabase"/></Cell> 
    <Cell ss:StyleID="s1254"><Data ss:Type="String">VolumeUnits</Data><NamedCell 
     ss:Name="_xlnm._FilterDatabase_7"/><NamedCell ss:Name="_FilterDatabase"/></Cell> 
    <Cell ss:StyleID="s1254"><Data ss:Type="String">CurrencyUnit</Data><NamedCell 
     ss:Name="_xlnm._FilterDatabase_7"/><NamedCell ss:Name="_FilterDatabase"/></Cell> 
    <Cell ss:StyleID="s1254"><Data ss:Type="String">AreaMeasurement</Data><NamedCell 
     ss:Name="_xlnm._FilterDatabase_7"/><NamedCell ss:Name="_FilterDatabase"/></Cell> 
    <Cell ss:StyleID="s1254"><Data ss:Type="String">ExternalSystem</Data><NamedCell 
     ss:Name="_xlnm._FilterDatabase_7"/><NamedCell ss:Name="_FilterDatabase"/></Cell> 
    <Cell ss:StyleID="s1254"><Data ss:Type="String">ExternalProjectObject</Data><NamedCell 
     ss:Name="_xlnm._FilterDatabase_7"/><NamedCell ss:Name="_FilterDatabase"/></Cell> 
    <Cell ss:StyleID="s1254"><Data ss:Type="String">ExternalProjectIdentifier</Data><NamedCell 
     ss:Name="_xlnm._FilterDatabase_7"/><NamedCell ss:Name="_FilterDatabase"/></Cell> 
    <Cell ss:StyleID="s1254"><Data ss:Type="String">ExternalSiteObject</Data><NamedCell 
     ss:Name="_xlnm._FilterDatabase_7"/><NamedCell ss:Name="_FilterDatabase"/></Cell> 
    <Cell ss:StyleID="s1254"><Data ss:Type="String">ExternalSiteIdentifier</Data><NamedCell 
     ss:Name="_xlnm._FilterDatabase_7"/><NamedCell ss:Name="_FilterDatabase"/></Cell> 
    <Cell ss:StyleID="s1254"><Data ss:Type="String">ExternalFacilityObject</Data><NamedCell 
     ss:Name="_xlnm._FilterDatabase_7"/><NamedCell ss:Name="_FilterDatabase"/></Cell> 
    <Cell ss:StyleID="s1254"><Data ss:Type="String">ExternalFacilityIdentifier</Data><NamedCell 
     ss:Name="_xlnm._FilterDatabase_7"/><NamedCell ss:Name="_FilterDatabase"/></Cell> 
    <Cell ss:StyleID="s1254"><Data ss:Type="String">Description</Data><NamedCell 
     ss:Name="_xlnm._FilterDatabase_7"/><NamedCell ss:Name="_FilterDatabase"/></Cell> 
    <Cell ss:StyleID="s1254"><Data ss:Type="String">ProjectDescription</Data><NamedCell 
     ss:Name="_xlnm._FilterDatabase_7"/><NamedCell ss:Name="_FilterDatabase"/></Cell> 
    <Cell ss:StyleID="s1254"><Data ss:Type="String">SiteDescription</Data><NamedCell 
     ss:Name="_xlnm._FilterDatabase_7"/><NamedCell ss:Name="_FilterDatabase"/></Cell> 
    <Cell ss:StyleID="s1254"><Data ss:Type="String">Phase</Data><NamedCell 
     ss:Name="_xlnm._FilterDatabase_7"/><NamedCell ss:Name="_FilterDatabase"/></Cell> 
    </Row> 
    </Table> 
    <WorksheetOptions xmlns="urn:schemas-microsoft-com:office:excel"> 
    <PageSetup> 
    <Header x:Margin="0.51180555555555551"/> 
    <Footer x:Margin="0.51180555555555551"/> 
    </PageSetup> 
    <Print> 
    <ValidPrinterInfo/> 
    <HorizontalResolution>300</HorizontalResolution> 
    <VerticalResolution>300</VerticalResolution> 
    </Print> 
    <TabColorIndex>43</TabColorIndex> 
    <Zoom>81</Zoom> 
    <FreezePanes/> 
    <FrozenNoSplit/> 
    <SplitHorizontal>1</SplitHorizontal> 
    <TopRowBottomPane>1</TopRowBottomPane> 
    <SplitVertical>1</SplitVertical> 
    <LeftColumnRightPane>1</LeftColumnRightPane> 
    <ActivePane>0</ActivePane> 
    <Panes> 
    <Pane> 
    <Number>3</Number> 
    </Pane> 
    <Pane> 
    <Number>1</Number> 
    </Pane> 
    <Pane> 
    <Number>2</Number> 
    </Pane> 
    <Pane> 
    <Number>0</Number> 
    <ActiveRow>20</ActiveRow> 
    </Pane> 
    </Panes> 
    <ProtectObjects>False</ProtectObjects> 
    <ProtectScenarios>False</ProtectScenarios> 
    <EnableSelection>NoSelection</EnableSelection> 
    </WorksheetOptions> 
    <AutoFilter x:Range="R1C1:R1C22" 
    xmlns="urn:schemas-microsoft-com:office:excel"> 
    </AutoFilter> 
</Worksheet> 
</Workbook> 

XDocument xmlDoc = XDocument.Load(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "\\Autodesk\\Revit\\Addins\\2013\\COBiev2.4Template.xml"); 

      /* ---- Get Facility Info ---- */ 
      var workbook = xmlDoc.Root.Element("Workbook"); 
      if (workbook != null) 
      { 
       var element = (from e in workbook.Elements("Worksheet") 
           where e.Attribute("ss:Name").Value == "Facility" 
           select e).FirstOrDefault(); 

       if (element != null) 
       { 
        TaskDialog.Show("Worksheet", "Found Facility"); 
        var tableNode = element.Element("Table"); 
        if (tableNode != null) 
        { 
         TaskDialog.Show("Table", "Found Table"); 
         tableNode.Add(new XElement("Row", 
          new XElement("Cell", 
           new XElement("Data", rDoc.ProjectInformation.Name) 
            ) 
         )); 
        } 
       } 
      } 
      else 
      { 
       TaskDialog.Show("Workbook", "Workbook not found"); 
      } 

未找到「工作簿」。我對此不甚瞭解,因爲這似乎應該起作用。

+0

你可以添加節點的例子,你想添加到包含的XML? – 2013-04-09 03:37:09

回答

0

找到node = xmlDoc這樣的節點怎麼樣? Element

並檢查它是節點的屬性。 Attributes

然後,添加像節點這樣的節點。 Add

0

的XElement是很容易使用,谷歌,你會發現一些有用的教程

var element = (from e in xmlDoc.Elements("Worksheet") 
       where e.Attribute("Name").Value == "Facility" 
       select e).FirstOrDefault(); 

if (element != null) 
{ 
    var tableNode = element.Element("Table"); 
    if (tableNode != null) 
     tableNode.Add(new XElement("Row")); 
} 
+0

這是將它添加到所有xmlDoc?或者它是否包含在新變量'element'中?我需要能夠將xmlDoc保存在此例程的最後,並將所有節點添加到該例程中。 – Bishop 2013-04-09 12:11:16

+0

您也會在'xmlDoc'中看到更改。 – MarcinJuraszek 2013-04-09 12:14:18

+0

這會將新節點添加到xmlDoc中 – 2013-04-09 12:42:12