Editing an XML document in C# -
i'm new c#. attempting edit spreadsheetxml 2003 document adding bunch of nodes different worksheets in it. here example of structure:
<workbook> <worksheet ss:name="contact"> <table> <column /> <row /> </table> </worksheet> <worksheet ss:name="facility"> <table> <column /> <row /> </table> </worksheet> </workbook>
what need load xml , insert new nodes @ places. i'm pretty sure know how create new xelement nodes under need (there's plenty of tutorials online that). i'm having trouble finding how find node , add nodes under it. far i've got:
xelement xmldoc = xelement.load(environment.getfolderpath(environment.specialfolder.applicationdata) + "\\autodesk\\revit\\addins\\2013\\cobiev2.4template.xml");
so, how find "facility" worksheet , add new row under table node in it? need able in such way @ end of routine can save xmldoc file out file system.
---- update ----- i'm not getting work. revised code , 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"); /* ---- facility info ---- */ var workbook = xmldoc.root.element("workbook"); if (workbook != null) { var element = (from e in workbook.elements("worksheet") 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. not understanding this, because seems should work.
how find node node = xmldoc.element
and check it's attribute node.attributes
then, add nodes node.add
Comments
Post a Comment