将XML内容保存到一个变量中
本文关键字:一个 变量 XML 保存 | 更新日期: 2023-09-27 18:12:01
下面的代码创建一个名为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();
}
您可以写入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())
你可以选择你需要的编码
只要稍微修改一下代码就可以得到想要的输出。您可以使用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();
}
我将在内存中创建一个DOM对象,而不是使用XmlWriter—理想情况下使用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();
我将与@Adil保持相同的解决方案。但是想要在代码中添加更改:
您可以使用XmlSerializer
类来代替手动编写xml对象(类的每个成员)。有了这个类,你可以将Deserialize
xml对象返回到类实例
第二-如果你想保持Employees
的数据在xml对象,也许以后改变一些东西在那里,比你可以使用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);
注:为了使序列化工作良好,Employee类和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;
}
}
不用流,你可以写一个StringBuilder实例:XmlWriter。Create-Methode (StringBuilder)