Monday, August 5, 2013

Working with XmlWriter in C#

The following XML code needed to be generated for the SharePoint 2007 Lists service.
<Batch OnError="Continue" ListVersion="1" 
ViewName="270C0508-A54F-4387-8AD0-49686D685EB2">
   <Method ID="1" Cmd="Delete">
      <Field Name='ID'>2</Field>
   </Method>
   <Method ID="2" Cmd="Delete">
      <Field Name='ID'>8</Field>
   </Method>
</Batch>
 It can be done using XmlWriter in C#
using (XmlWriter writer = XmlWriter.Create("test.xml"))
{
writer.WriteStartDocument();
writer.WriteStartElement(
"Batch");
writer.WriteAttributeString("OnError", "Continue");
writer.WriteAttributeString("ListVersion", "1");
writer.WriteStartElement("Method");
writer.WriteAttributeString("ID", "1");
writer.WriteAttributeString("Cmd", "New");
writer.WriteStartElement("Field");
writer.WriteAttributeString("Name", "ID");
writer.WriteString("Item ID");
writer.WriteEndElement();
writer.WriteEndElement();
writer.WriteEndElement();
writer.WriteEndDocument();
}

No comments:

Post a Comment