简体   繁体   中英

Editing an XML document in C#

I'm new to C#. I am attempting to edit a spreadsheetXML 2003 document by adding a bunch of nodes to the different worksheets in it. Here is an example of the structure:

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

What I need to do is to load this XML and insert new nodes at certain places. I'm pretty sure I know how to create new XElement with the nodes under it that I need (there's plenty of tutorials online for that). What I'm having trouble finding is how to find a node and add nodes under it. This is as far as I've got:

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

So, how do I find the "Facility" worksheet and add a new row under the table node in it? I need to be able to do this in such a way that at the end of my routine I can save the xmlDoc file back out to the file system.

---- UPDATE ----- I'm not getting this to work. Revised code and beginning of actual XML below.

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

The "Workbook" isn't being found. What am I not understanding about this, because this seems like it should work.

How about find a node like node = xmlDoc. Element

And check it's attribute by node. Attributes

Then, add nodes like node. Add

XElement is pretty easy to use, google it, you will find some useful tutorial

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM