2

I've got some xml node that I've pulled out and I'm using XmlWriter to create the relevant Xml file.

The XmlNode needs to go inside but I wasn't sure how to use the XmlNode to create the Elements inside.

Xml Format as below

<?xml version="1.0" encoding="UTF-8"?>
<tasks>
   <task Id="113" Date="2013-03-12T13:55:09" NodeId="1842" TotalWords="11">

   </task>
</tasks>

XmlWriter codes

using (XmlWriter writer = XmlWriter.Create(MapPath(xmlFilePath)))
                {

                    writer.WriteStartDocument();
                    writer.WriteStartElement("Tasks");

                    writer.WriteStartElement("Task");
                    writer.WriteAttributeString("Id", t.Id.ToString());
                    writer.WriteAttributeString("NodeId", t.Node.Id.ToString());

                    // The XmlNode needs to be inside here //

                    writer.WriteEndElement();

                    writer.WriteEndElement();
                    writer.WriteEndDocument();


                }

Details of the XmlNode that I've pulled out

<Page id="1842" parentID="1840" level="5" writerID="14" creatorID="14" nodeType="1051" template="1839" sortOrder="0" createDate="2013-03-11T14:53:47" updateDate="2013-03-12T14:03:55" nodeName="Ujian sub-halaman" urlName="ujian-sub-halaman" writerName="John John" creatorName="John John" path="-1,1057,1631,1156,1840,1842" isDoc="">
         <pageContent />
         <heroHeaderImage />
         <topModule>-1</topModule>
         <bottomModule>-1</bottomModule>
         <rHTopModule />
         <headerQuickLinks>
            <data />
         </headerQuickLinks>
         <rHBottomModule>-1</rHBottomModule>
         <pageTitle>Ujian sub-halaman</pageTitle>
         <umbracoNaviHide>0</umbracoNaviHide>
         <pageHeader><![CDATA[Test sub-page]]></pageHeader>
         <showAddThis>0</showAddThis>
         <alternativeTitle>Test sub-page</alternativeTitle>
         <smallPrint>0</smallPrint>
         <pageImage />
         <siteName />
         <metaDescription />
         <metaKeywords />
         <metaCopyright />
         <defaultEmail />
         <googleUrchin />
         <facebook />
         <googlePlus />
         <linkedIn />
         <twitter />
         <youtube />
      </Page>
1
  • LINQ to XML is much easier to use than XmlWriter. Commented Mar 12, 2013 at 16:41

1 Answer 1

4

I'm not sure if this is what you mean, but assuming you have an XmlNode - let's call it the_node then you could just use the WriteRaw method of XmlWriter to embed the markup of the node into the output stream of writer.

using (XmlWriter writer = XmlWriter.Create(MapPath(xmlFilePath)))
{

   writer.WriteStartDocument();
   writer.WriteStartElement("Tasks");

   writer.WriteStartElement("Task");
   writer.WriteAttributeString("Id", t.Id.ToString());
   writer.WriteAttributeString("NodeId", t.Node.Id.ToString());

  // The XmlNode needs to be inside here //
   writer.WriteRaw(the_node.OuterXml);

   writer.WriteEndElement();

   writer.WriteEndElement();
   writer.WriteEndDocument();


}
Sign up to request clarification or add additional context in comments.

3 Comments

I recomment to use "the_node.WriteTo(writer)" instead of "writer.WriteRaw(the_node.OuterXml)".
@Frankenstein Why?
@NisimNaim WriteTo will verify the submitted xml code before it will be written. In contrast WriteRaw will skip this step. In this example the OuterXml of a node is always correct. But with other data you might risk writing invalid xml code

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.