I need read XElements from xml and add new XElements to exists xml. Firs I get XElements from xml, after I want add newXElements to xElements and update exists xml file. Then I call XmlWriter I have a Exception: "The process cannot access the file because it is being used by another process". Where is error? How append XElements to xml in stream?
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 q = XElement.Load(path);
q.save(Path);
I'd recommend trying those out as an alternative to streamreading/writing to XElement.
In regards to your error, have you stepped through the code and identified where it's failing?
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.