EDN Admin
Well-known member
<div style="color:#000000; font-family:Verdana,Arial,Helvetica,sans-serif; font-size:67%; background-color:#ffffff; margin:8px
Hi.
Im trying to remove a XNode from a xml file.
the cod to remove the specified element looks like follow:
<div style="color:Black; background-color:White
<pre style="font-size:11px; border:1px solid #d0d0d0 <span style="color:Blue public <span style="color:Blue static XDocument DeleteMetaData(Stream stream, <span style="color:Blue string AuditplanId, <span style="color:Blue string metaDataId)
{
XDocument document;
<span style="color:Blue using (<span style="color:Blue var reader = <span style="color:Blue new StreamReader(stream))
{
<span style="color:Blue string payload = reader.ReadToEnd().Replace(<span style="color:#A31515 , <span style="color:#A31515 );
document = XDocument.Parse(payload);
XElement ele = document.Descendants(<span style="color:#A31515 "Auditplan").Where(x => x.Element(<span style="color:#A31515 "ID").Value == AuditplanId).FirstOrDefault().Element(<span style="color:#A31515 "MetaData").Elements(<span style="color:#A31515 "MetaData").Where(x => x.Element(<span style="color:#A31515 "ID").Value == metaDataId).FirstOrDefault();
document.Descendants(<span style="color:#A31515 "Auditplan").Where(x => x.Element(<span style="color:#A31515 "ID").Value == AuditplanId).FirstOrDefault().Element(<span style="color:#A31515 "MetaData").Elements(<span style="color:#A31515 "MetaData").Where(x => x.Element(<span style="color:#A31515 "ID").Value == metaDataId).FirstOrDefault().Remove();
stream.Position = 0;
document.Save(stream);
}
<span style="color:Blue return document;
}
[/code]
where the stream parameter is:
<div style="color:Black; background-color:White
<pre style="font-size:11px; border:1px solid #d0d0d0 IsolatedStorageFile StorageFile=IsolatedStorageFile.GetUserStoreForApplication();
<span style="color:Blue using (<span style="color:Blue var stream = StorageFile.OpenFile(DATA_FILE_PATH,
System.IO.FileMode.Open, FileAccess.ReadWrite))
{
System.Xml.Linq.XDocument document = MobileQMUtilities.Utilities.DeleteMetaData(stream, NavigationContext.QueryString[<span style="color:#A31515 "ID"], args.MetaDataToSave.ID.ToString());
}
[/code]
so, after the call of DeleteMetaData, the the desired element is removed from the document, and returned document looks like it should be, without removed document.
But when I open the file for read/write again
<div style="color:Black; background-color:White
<pre style="font-size:11px; border:1px solid #d0d0d0 System.Xml.XmlException was unhandled
Message=Data at the root level <span style="color:Blue is invalid. Line 9980, position 16.
LineNumber=9980
LinePosition=16
SourceUri=<span style="color:#A31515 ""
StackTrace:
at System.Xml.XmlTextReaderImpl.Throw(Exception e)
at System.Xml.XmlTextReaderImpl.Throw(Int32 res, String resString, String[] args)
at System.Xml.XmlTextReaderImpl.Throw(Int32 res, String resString)
at System.Xml.XmlTextReaderImpl.ParseRootLevelWhitespace()
at System.Xml.XmlTextReaderImpl.ParseDocumentContent()
at System.Xml.XmlTextReaderImpl.Read()
at System.Xml.Linq.XContainer.ReadContentFrom(XmlReader r)
at System.Xml.Linq.XContainer.ReadContentFrom(XmlReader r, LoadOptions o)
at System.Xml.Linq.XDocument.Load(XmlReader reader, LoadOptions options)
at System.Xml.Linq.XDocument.Parse(String text, LoadOptions options)
at System.Xml.Linq.XDocument.Parse(String text)
at MobileQMUtilities.Utilities.GetMetaDataList(Stream stream, String auditplanId, Types type)
at mobileQM.Views.AuditPhotos.crtlAddMetaData_AddMetaDataToAuditplan(AddMetaDataToAuditPlanArgs args)
at mobileQM.Controls.AddMetaData.btnDelete_Click(Object sender, RoutedEventArgs e)
at System.Windows.Controls.Primitives.ButtonBase.OnClick()
at System.Windows.Controls.Button.OnClick()
at System.Windows.Controls.Primitives.ButtonBase.OnMouseLeftButtonUp(MouseButtonEventArgs e)
at System.Windows.Controls.Control.OnMouseLeftButtonUp(Control ctrl, EventArgs e)
at MS.Internal.JoltHelper.FireEvent(IntPtr unmanagedObj, IntPtr unmanagedObjArgs, Int32 argsTypeIndex, String eventName)
[/code]
<div style="color:Black; background-color:White
<pre style="font-size:11px; border:1px solid #d0d0d0 OR
[/code]
<div style="color:Black; background-color:White
<pre style="font-size:11px; border:1px solid #d0d0d0 System.Xml.XmlException was unhandled
Message=Unexpected end tag. Line 9974, position 24.
LineNumber=9974
LinePosition=24
SourceUri=<span style="color:#A31515 ""
StackTrace:
at System.Xml.XmlTextReaderImpl.Throw(Exception e)
at System.Xml.XmlTextReaderImpl.Throw(Int32 res, String resString, String[] args)
at System.Xml.XmlTextReaderImpl.Throw(Int32 pos, Int32 res, String resString)
at System.Xml.XmlTextReaderImpl.ParseDocumentContent()
at System.Xml.XmlTextReaderImpl.Read()
at System.Xml.Linq.XContainer.ReadContentFrom(XmlReader r)
at System.Xml.Linq.XContainer.ReadContentFrom(XmlReader r, LoadOptions o)
at System.Xml.Linq.XDocument.Load(XmlReader reader, LoadOptions options)
at System.Xml.Linq.XDocument.Parse(String text, LoadOptions options)
at System.Xml.Linq.XDocument.Parse(String text)
at MobileQMUtilities.Utilities.GetMetaDataList(Stream stream, String auditplanId, Types type)
at mobileQM.Views.AuditPhotos.crtlAddMetaData_AddMetaDataToAuditplan(AddMetaDataToAuditPlanArgs args)
at mobileQM.Controls.AddMetaData.btnDelete_Click(Object sender, RoutedEventArgs e)
at System.Windows.Controls.Primitives.ButtonBase.OnClick()
at System.Windows.Controls.Button.OnClick()
at System.Windows.Controls.Primitives.ButtonBase.OnMouseLeftButtonUp(MouseButtonEventArgs e)
at System.Windows.Controls.Control.OnMouseLeftButtonUp(Control ctrl, EventArgs e)
at MS.Internal.JoltHelper.FireEvent(IntPtr unmanagedObj, IntPtr unmanagedObjArgs, Int32 argsTypeIndex, String eventName)
[/code]
or whatever, depending of the element and his position in the file, I have deleted.
Thats because a part of the xml document is added after root elements closing tag.
So what could be the reason for this, and how to solve this issue?
Thanks and recards.
P.S.
There is no problem to add new elements to the file. The only problem is removing something in the file.
View the full article
Hi.
Im trying to remove a XNode from a xml file.
the cod to remove the specified element looks like follow:
<div style="color:Black; background-color:White
<pre style="font-size:11px; border:1px solid #d0d0d0 <span style="color:Blue public <span style="color:Blue static XDocument DeleteMetaData(Stream stream, <span style="color:Blue string AuditplanId, <span style="color:Blue string metaDataId)
{
XDocument document;
<span style="color:Blue using (<span style="color:Blue var reader = <span style="color:Blue new StreamReader(stream))
{
<span style="color:Blue string payload = reader.ReadToEnd().Replace(<span style="color:#A31515 , <span style="color:#A31515 );
document = XDocument.Parse(payload);
XElement ele = document.Descendants(<span style="color:#A31515 "Auditplan").Where(x => x.Element(<span style="color:#A31515 "ID").Value == AuditplanId).FirstOrDefault().Element(<span style="color:#A31515 "MetaData").Elements(<span style="color:#A31515 "MetaData").Where(x => x.Element(<span style="color:#A31515 "ID").Value == metaDataId).FirstOrDefault();
document.Descendants(<span style="color:#A31515 "Auditplan").Where(x => x.Element(<span style="color:#A31515 "ID").Value == AuditplanId).FirstOrDefault().Element(<span style="color:#A31515 "MetaData").Elements(<span style="color:#A31515 "MetaData").Where(x => x.Element(<span style="color:#A31515 "ID").Value == metaDataId).FirstOrDefault().Remove();
stream.Position = 0;
document.Save(stream);
}
<span style="color:Blue return document;
}
[/code]
where the stream parameter is:
<div style="color:Black; background-color:White
<pre style="font-size:11px; border:1px solid #d0d0d0 IsolatedStorageFile StorageFile=IsolatedStorageFile.GetUserStoreForApplication();
<span style="color:Blue using (<span style="color:Blue var stream = StorageFile.OpenFile(DATA_FILE_PATH,
System.IO.FileMode.Open, FileAccess.ReadWrite))
{
System.Xml.Linq.XDocument document = MobileQMUtilities.Utilities.DeleteMetaData(stream, NavigationContext.QueryString[<span style="color:#A31515 "ID"], args.MetaDataToSave.ID.ToString());
}
[/code]
so, after the call of DeleteMetaData, the the desired element is removed from the document, and returned document looks like it should be, without removed document.
But when I open the file for read/write again
<div style="color:Black; background-color:White
<pre style="font-size:11px; border:1px solid #d0d0d0 System.Xml.XmlException was unhandled
Message=Data at the root level <span style="color:Blue is invalid. Line 9980, position 16.
LineNumber=9980
LinePosition=16
SourceUri=<span style="color:#A31515 ""
StackTrace:
at System.Xml.XmlTextReaderImpl.Throw(Exception e)
at System.Xml.XmlTextReaderImpl.Throw(Int32 res, String resString, String[] args)
at System.Xml.XmlTextReaderImpl.Throw(Int32 res, String resString)
at System.Xml.XmlTextReaderImpl.ParseRootLevelWhitespace()
at System.Xml.XmlTextReaderImpl.ParseDocumentContent()
at System.Xml.XmlTextReaderImpl.Read()
at System.Xml.Linq.XContainer.ReadContentFrom(XmlReader r)
at System.Xml.Linq.XContainer.ReadContentFrom(XmlReader r, LoadOptions o)
at System.Xml.Linq.XDocument.Load(XmlReader reader, LoadOptions options)
at System.Xml.Linq.XDocument.Parse(String text, LoadOptions options)
at System.Xml.Linq.XDocument.Parse(String text)
at MobileQMUtilities.Utilities.GetMetaDataList(Stream stream, String auditplanId, Types type)
at mobileQM.Views.AuditPhotos.crtlAddMetaData_AddMetaDataToAuditplan(AddMetaDataToAuditPlanArgs args)
at mobileQM.Controls.AddMetaData.btnDelete_Click(Object sender, RoutedEventArgs e)
at System.Windows.Controls.Primitives.ButtonBase.OnClick()
at System.Windows.Controls.Button.OnClick()
at System.Windows.Controls.Primitives.ButtonBase.OnMouseLeftButtonUp(MouseButtonEventArgs e)
at System.Windows.Controls.Control.OnMouseLeftButtonUp(Control ctrl, EventArgs e)
at MS.Internal.JoltHelper.FireEvent(IntPtr unmanagedObj, IntPtr unmanagedObjArgs, Int32 argsTypeIndex, String eventName)
[/code]
<div style="color:Black; background-color:White
<pre style="font-size:11px; border:1px solid #d0d0d0 OR
[/code]
<div style="color:Black; background-color:White
<pre style="font-size:11px; border:1px solid #d0d0d0 System.Xml.XmlException was unhandled
Message=Unexpected end tag. Line 9974, position 24.
LineNumber=9974
LinePosition=24
SourceUri=<span style="color:#A31515 ""
StackTrace:
at System.Xml.XmlTextReaderImpl.Throw(Exception e)
at System.Xml.XmlTextReaderImpl.Throw(Int32 res, String resString, String[] args)
at System.Xml.XmlTextReaderImpl.Throw(Int32 pos, Int32 res, String resString)
at System.Xml.XmlTextReaderImpl.ParseDocumentContent()
at System.Xml.XmlTextReaderImpl.Read()
at System.Xml.Linq.XContainer.ReadContentFrom(XmlReader r)
at System.Xml.Linq.XContainer.ReadContentFrom(XmlReader r, LoadOptions o)
at System.Xml.Linq.XDocument.Load(XmlReader reader, LoadOptions options)
at System.Xml.Linq.XDocument.Parse(String text, LoadOptions options)
at System.Xml.Linq.XDocument.Parse(String text)
at MobileQMUtilities.Utilities.GetMetaDataList(Stream stream, String auditplanId, Types type)
at mobileQM.Views.AuditPhotos.crtlAddMetaData_AddMetaDataToAuditplan(AddMetaDataToAuditPlanArgs args)
at mobileQM.Controls.AddMetaData.btnDelete_Click(Object sender, RoutedEventArgs e)
at System.Windows.Controls.Primitives.ButtonBase.OnClick()
at System.Windows.Controls.Button.OnClick()
at System.Windows.Controls.Primitives.ButtonBase.OnMouseLeftButtonUp(MouseButtonEventArgs e)
at System.Windows.Controls.Control.OnMouseLeftButtonUp(Control ctrl, EventArgs e)
at MS.Internal.JoltHelper.FireEvent(IntPtr unmanagedObj, IntPtr unmanagedObjArgs, Int32 argsTypeIndex, String eventName)
[/code]
or whatever, depending of the element and his position in the file, I have deleted.
Thats because a part of the xml document is added after root elements closing tag.
So what could be the reason for this, and how to solve this issue?
Thanks and recards.
P.S.
There is no problem to add new elements to the file. The only problem is removing something in the file.
View the full article