在C#中编辑XML文档

时间:2013-04-09 03:22:38

标签: c# xml linq-to-xml

我是C#的新手。我试图通过向其中的不同工作表添加一堆节点来编辑spreadsheetXML 2003文档。以下是结构示例:

<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");

那么,我如何找到“Facility”工作表并在其中的表节点下添加一个新行?我需要能够这样做,以便在我的例程结束时,我可以将xmlDoc文件保存回文件系统。

----更新----- 我没有让这个工作。下面修改了代码和实际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");
            }

找不到“工作簿”。我对此并不了解,因为这似乎应该有效。

2 个答案:

答案 0 :(得分:0)

如何找到node = xmlDoc。Element

之类的节点

按节点检查它的属性。Attributes

然后,添加节点。Add

等节点

答案 1 :(得分:0)

XElement非常易于使用,google it,你会发现一些有用的教程

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"));
}