将XML内容保存到变量中

时间:2013-05-04 05:51:46

标签: c# xml

以下代码创建名为 employees.xml 的物理XML文件。但我不想创建一个物理文件。相反,我想将XML内容保存到变量中。如何修改以下代码以将XML数据保存到变量中而不是实际创建它?

using (XmlWriter writer = XmlWriter.Create("employees.xml"))
{
    writer.WriteStartDocument();
    writer.WriteStartElement("Employees");

    foreach (Employee employee in employees)
    {
        writer.WriteStartElement("Employee");

        writer.WriteElementString("ID", employee.Id.ToString());
        writer.WriteElementString("FirstName", employee.FirstName);
        writer.WriteElementString("LastName", employee.LastName);
        writer.WriteElementString("Salary", employee.Salary.ToString());

        writer.WriteEndElement();
    }

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

5 个答案:

答案 0 :(得分:3)

您可以写入MemoryStream并使用

var stream = new MemoryStream();
var writer = XmlWriter.Create(stream);

您的代码将被修改为

var stream = new MemoryStream();    
using (XmlWriter writer = XmlWriter.Create(stream))
{
    writer.WriteStartDocument();
    writer.WriteStartElement("Employees");

    foreach (Employee employee in employees)
    {
       writer.WriteStartElement("Employee");
       writer.WriteElementString("Id", employee.Id.ToString());
       writer.WriteElementString("FirstName", employee.FirstName);
       writer.WriteElementString("LastName", employee.LastName);
       writer.WriteElementString("Salary", employee.Salary);
       writer.WriteEndElement();
    }

    writer.WriteEndElement();
    writer.WriteEndDocument();
}
string strXml = System.Text.ASCIIEncoding.UTF8.GetString(stream.ToArray())

您可以选择所需的编码。

答案 1 :(得分:1)

只需对代码进行一次小修改即可获得所需的输出。您可以使用StringWriter类,最终的XML将保存在xmlString变量中。

string xmlString = null;
using (StringWriter sw = new StringWriter())
{
    XmlTextWriter writer = new XmlTextWriter(sw);
    writer.Formatting = Formatting.Indented; // if you want it indented

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

    foreach (Employee employee in employees)
    {
        writer.WriteStartElement("Employee");

        writer.WriteElementString("ID", employee.Id.ToString());
        writer.WriteElementString("FirstName", employee.FirstName);
        writer.WriteElementString("LastName", employee.LastName);
        writer.WriteElementString("Salary", employee.Salary.ToString());

        writer.WriteEndElement();
    }

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

    xmlString = sw.ToString();
}

答案 2 :(得分:1)

我不是使用XmlWriter,而是在内存中创建一个DOM对象 - 理想情况下使用LINQ to XML,这样可以将整个事物表达为查询:

var xml = new XDocument(new XElement("Employees"),
     employees.Select(e => new XElement("Employee",
         new XElement("ID", e.Id),
         new XElement("FirstName", employee.FirstName),
         new XElement("LastName", employee.LastName),
         new XElement("Salary", employee.Salary))));

您现在可以操作此XML(添加或删除元素等),当您想将其保存到文件中(或通过网络流式传输或其他任何内容时,您可以轻松地执行此操作)。

如果您想在变量中使用字符串表示,则可以使用ToString

var xmlAsText = xml.ToString();

答案 3 :(得分:1)

我将继续使用@Adil的相同解决方案。但是想在代码中添加更改:

您可以使用XmlSerializer类,而不是手动编写xml对象(类的每个成员)。使用此类,您可以Deserialize xml对象返回类实例

第二 - 如果你想在xml对象中保留Employees的数据,以后可能会在那里更改某些内容,那么你可以使用XmlDocument。这是一个代码:

XmlSerializer _xSerialize = new XmlSerializer(_employees.GetType());
MemoryStream _mstream = new MemoryStream();

using (XmlWriter writer = XmlWriter.Create(_mstream))
{
    _xSerialize.Serialize(_mstream, _workers );
}

XmlDocument document = new XmlDocument();
document.Load(_mstream);

P.S。对于序列化工作正常,两个类Employees和Employee必须具有默认构造函数(没有参数),并且您希望xml的属性必须是公共的。

这是我用于测试的类:

public class Employees
{
    public List<Employee> Workers { get; set; }

    public Employees()
    {
        this.Workers = new List<Employee>();
    }
}

public class Employee
{
    public Int32 ID{ get; set; }
    public string FirstName{ get; set; }
    public string LastName{ get; set; }
    public float Salary{ get; set; }

    public Employee() { }

    public Employee(Int32 id, string fname, string lname, float salary)
    {
        this.ID = id;
        this.FirstName = fname;
        this.LastName = lname;
        this.Salary = salary;
    }
}

答案 4 :(得分:0)

您可以写入StringBuilder实例:XmlWriter.Create-Methode (StringBuilder)

,而不是使用某些流