[英]Add XElements to exists xml file
I need read XElements from xml and add new XElements to exists xml. 我需要从xml读取XElement并将新的XElement添加到存在的xml中。 Firs I get XElements from xml, after I want add newXElements to xElements and update exists xml file.
首先,我想从xml中获取XElement,然后将newXElements添加到xElements并更新存在的xml文件。 Then I call XmlWriter I have a Exception: "The process cannot access the file because it is being used by another process".
然后我给XmlWriter打电话,我有一个例外:“该进程无法访问该文件,因为该文件正在被另一个进程使用”。 Where is error?
错误在哪里? How append XElements to xml in stream?
如何将XElements附加到流中的xml?
IEnumerable<XElement> newXElements = GetNewXElements();
IEnumerable<XElement> xElements = GetXElements(path, "MyElement");
using (XmlWriter xw = XmlWriter.Create(path, settings))
{
// Write to xml code
}
public static IEnumerable<XElement> GetXElements(string inputUrl, string elementName)
{
using (XmlReader reader = XmlReader.Create(inputUrl))
{
reader.MoveToContent();
while (reader.Read())
{
if (reader.NodeType == XmlNodeType.Element)
{
if (reader.Name == elementName)
{
XElement el = XNode.ReadFrom(reader) as XElement;
if (el != null)
{
yield return el;
}
}
}
}
}
}
XElement has it's own load method, just as it has it's own Save XElement拥有自己的加载方法,就像它拥有自己的Save一样
XElement q = XElement.Load(path);
q.save(Path);
I'd recommend trying those out as an alternative to streamreading/writing to XElement. 我建议尝试一下这些方法,以替代对XElement进行流读取/写入的方法。
In regards to your error, have you stepped through the code and identified where it's failing? 关于您的错误,您是否单步执行代码并确定失败的地方?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.