如何创建XML字符串而不是使用字符串生成器?

Nic*_*ick 4 c# xml asp.net sharepoint

我使用下面的代码(本例简化)将数据发布到SharePoint列表

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);
Run Code Online (Sandbox Code Playgroud)

正如您所看到的,我使用的是一个不理想的字符串构建器,所以我想知道应该使用什么来代替创建XML字符串?

提前致谢.

Maa*_*ate 18

您可以使用Linq to XML,请查看类似的内容:http://www.hookedonlinq.com/LINQtoXML5MinuteOverview.ashx.

例如,这段代码:

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();
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

将输出:

<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>
Run Code Online (Sandbox Code Playgroud)


Ser*_*ier 8

  1. 创建一个模仿XML模式的类.
  2. 实例化类并填充其属性(属性,元素)
  3. 使用XmlSerialization以字符串或流的形式生成XML片段.

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);
Run Code Online (Sandbox Code Playgroud)


Sco*_*eed 3

如果您编写 xml 为什么不使用仅向前的 XmlWriter?http://msdn.microsoft.com/en-us/library/system.xml.xmlwriter.aspx它是为创建 xml 结构而设计的。