System.IO.Stream stream = Request.InputStream;
stream.Position = 0;//重置流的位置,以便我们可以从头读取
TextReader tread = new StreamReader(stream, System.Text.Encoding.GetEncoding("gb2312"));
XElement doc = XElement.Load(tread);
IEnumerable<XElement> xElement = from xx in doc.Descendants("File")
select xx;
就这么几步 搞了 我两天 终于好了
还是基础方法没找对啊
记录下
//附加文件创建的操作
string path = @"D:/pan/数据接收服务/SaveAlarmRecord.xml";
System.IO.FileInfo fInfo = new System.IO.FileInfo(path);
if (fInfo.Exists)
{
XElement doc_xml = XElement.Load(path);
doc_xml.Add(xElement);
doc_xml.Save(path);
}
else
{
FileStream fs = fInfo.Create();
string xx = "<?xml version='1.0' encoding='gb2312' ?><Alarm></Alarm>";
System.Text.Encoding GB2312 = System.Text.Encoding.GetEncoding("GB2312");
byte[] data = System.Text.Encoding.GetEncoding("GB2312").GetBytes(xx);
fs.Write(data, 0, data.Length);
fs.Flush();
fs.Close();
fs.Dispose();
XElement doc_xml = XElement.Load(path);
doc_xml.Add(xElement);
doc_xml.Save(path);
}
本文介绍了如何使用C#处理XML文件,包括从输入流中加载并解析XML文档、添加新的XML元素到现有文件中,以及如何创建新的XML文件并保存到指定路径。文章通过实例演示了整个过程。
2893

被折叠的 条评论
为什么被折叠?



