2 votes

Ajout de XmlNode dans XmlWriter

J'ai un nœud xml que j'ai extrait et j'utilise XmlWriter pour créer le fichier Xml correspondant.

Le XmlNode doit aller à l'intérieur mais je ne savais pas comment utiliser le XmlNode pour créer les éléments à l'intérieur.

Format Xml comme ci-dessous

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

   </task>
</tasks>

Codes XmlWriter

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

                }

Détails du XmlNode que j'ai retiré

<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>

4voto

Miky Dinescu Points 22380

Je ne suis pas sûr que ce soit ce que vous voulez dire, mais en supposant que vous ayez un XmlNode - appelons-le the_node alors vous pourriez simplement utiliser le WriteRaw de XmlWriter pour intégrer le balisage du nœud dans le flux de sortie de l'application 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();

}

Prograide.com

Prograide est une communauté de développeurs qui cherche à élargir la connaissance de la programmation au-delà de l'anglais.
Pour cela nous avons les plus grands doutes résolus en français et vous pouvez aussi poser vos propres questions ou résoudre celles des autres.

Powered by:

X