Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create XML string instead of using string builder?

I am using the code below (simplified for this example) to post data to a SharePoint list

StringBuilder customerDoc = new StringBuilder();

customerDoc.Append("<Method ID='1' Cmd='New'>");
customerDoc.Append("<Field Name='Name'>" + Name + "</Field>");
customerDoc.Append("<Field Name='Age'>" + age + "</Field>");
customerDoc.Append("<Field Name='City'>" + city + "</Field>");
customerDoc.Append("<Field Name='Country'>" + country + "</Field>");

customerDoc.Append("</Method>");

XmlDocument xDoc = new XmlDocument();
XmlElement xBatch = xDoc.CreateElement("Batch");
xBatch.SetAttribute("OnError", "Continue");

xBatch.InnerXml = sb_method.ToString();

XmlNode xn_return = sharePoint.listsObj.UpdateListItems(ConfigurationManager.AppSettings["SaveCustomer"].ToString(), xBatch);

As you can see I am using a stringbuilder which isn't ideal so I wonder what I should use instead to create an XML string?

Thanks in advance.

like image 367
Nick Avatar asked Mar 29 '11 07:03

Nick


People also ask

What is XML string?

String. xml file contains all the strings which will be used frequently in Android project. String. xml file present in the values folder which is sub folder of res folder in project structure.In Android Studio, we have many Views such as TextView,Button,EditText,CheckBox,RadioButton etc.


2 Answers

You could use Linq to XML, please check out something like: http://www.hookedonlinq.com/LINQtoXML5MinuteOverview.ashx.

For example, this code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Dynamic;
using System.Xml.Linq;

namespace Test
{
    class Program
    {
        static void Main(string[] args)
        {
            String name = "Morten";
            Int32 age = 30;
            String city = "Copenhagen";
            String country = "Denmark";

            XElement xml = new XElement("Method", 
                new XAttribute("ID", 1), 
                new XAttribute("Cmd", "New"),
                new XElement("Field", 
                    new XAttribute("Name", "Name"), 
                    name),
                new XElement("Field", 
                    new XAttribute("Name", "Age"), 
                    age),
                new XElement("Field", 
                    new XAttribute("Name", "City"), 
                    city),
                new XElement("Field", 
                    new XAttribute("Name", "Country"), 
                    country)
            );

            Console.WriteLine(xml);
            Console.ReadKey();
        }
    }
}

Will output:

<Method ID="1" Cmd="New">
  <Field Name="Name">Morten</Field>
  <Field Name="Age">30</Field>
  <Field Name="City">Copenhagen</Field>
  <Field Name="Country">Denmark</Field>
</Method>
like image 185
Maate Avatar answered Nov 15 '22 15:11

Maate


  1. Create a class that mimics your XML schema.
  2. Instantiate the class and fill its properties (attributes, elements)
  3. Use XmlSerialization to generate an XML fragment either as a string or a stream.

d

public class Method
{
  [XmlAttribute()]
  public int ID {get;set;}

  [XmlAttribute()]
  public string Cmd {get;set;}

  public string Name {get;set;}
  public int Age {get;set;}
  public string City {get;set;}
  public string Country {get;set;}
}

public class Batch
{
  public Method Method { get; set; }
}

public static string ToXml(object Doc)
{
  try
  {
    // Save to XML string
    XmlSerializer ser = new XmlSerializer(Doc.GetType());
    var sb = new StringBuilder();
    using (var writer = XmlWriter.Create(sb))
    {
      ser.Serialize(writer, Doc);
    }
    return sb.ToString();
  }
  catch (Exception ex)
  { // Weird!
    ProcessException();
  }
}

var batch = new Batch();
batch.Method = new Method { ID=..., Cmd=..., ...};

var xml = ToXml(batch);
like image 29
Serge Wautier Avatar answered Nov 15 '22 14:11

Serge Wautier