问题
I have a little problem. I try to add an element in XML with this code:
using (IsolatedStorageFile myStore = IsolatedStorageFile.GetUserStoreForApplication())
{
if (!myStore.FileExists("categorie_settings.xml"))
{
using (IsolatedStorageFileStream myStream = new IsolatedStorageFileStream("categorie_settings.xml", FileMode.Create, myStore))
{
XNamespace _name = "";
XDocument new_doc = new XDocument(
new XDeclaration("1.0", "utf-8", ""),
new XElement(_name + "Root",
new XElement("Row",
new XElement("Nome", query.FirstOrDefault().Nome.ToString()),
new XElement("Tipo", query.FirstOrDefault().Tipo.ToString()),
new XElement("URL", query.FirstOrDefault().URL.ToString()),
new XElement("Err", "Errore1")
)));
new_doc.Save(myStream);
}
}
else
{
using (IsolatedStorageFileStream myStream = new IsolatedStorageFileStream("categorie_settings.xml", FileMode.Open, myStore))
{
XDocument doc1 = XDocument.Load(myStream);
doc1.Element("Root").Add(
new XElement("Row",
new XElement("Nome", query.FirstOrDefault().Nome.ToString()),
new XElement("Tipo", query.FirstOrDefault().Tipo.ToString()),
new XElement("URL", query.FirstOrDefault().URL.ToString()),
new XElement("Err", "Errore2")));
doc1.Save(myStream);
}
}
In the second Using I want to add an item to the XML file but the result after 2 call is this:
<?xml version="1.0" encoding="utf-8"?>
<Root>
<Row>
<Nome>Homepage</Nome>
<Tipo>News</Tipo>
<URL>/web/ansait_web_rss_homepage.xml</URL>
<Err>Errore1</Err>
</Row>
</Root><?xml version="1.0" encoding="utf-8"?>
<Root>
<Row>
<Nome>Homepage</Nome>
<Tipo>News</Tipo>
<URL>/web/ansait_web_rss_homepage.xml</URL>
<Err>Errore1</Err>
</Row>
<Row>
<Nome>Cronache</Nome>
<Tipo>News</Tipo>
<URL>/web/notizie/rubriche/cronaca/cronaca_rss.xml</URL>
<Err>Errore2</Err>
</Row>
</Root>
It seems that adds to the file before the entire file plus the new element. How can I make just add the element?
回答1:
Apologies for the false start (deleted). Here's a snippet that would go into the second using-statement in your example. Note how we select the first Row element (firstRow) and then AddBeforeSelf. This assumes you want the new element at the top of the list.
// See update, below, for corrected version
XDocument doc1 = XDocument.Load( myStream );
var root = doc1.Element( "Root" );
var rows = root.Descendants( "Row" );
var firstRow = rows.First();
firstRow.AddBeforeSelf(
new XElement( "Row",
new XElement( "Nome", "Homepage2" ),
new XElement( "Tipo", "News2" ),
new XElement( "URL", "/web/xml2" ),
new XElement( "Err", "Errore2" ) ) );
doc1.Save( myStream );
Updated: as the OP points out in the comments, he wanted the new element appended, not prepended. Additionally he gets an exception on subsequent invocations. That exception is due to XDocument.Save( Stream stream ) appending to the stream, so you wind up with two XML documents in the file (not well-formed XML). This following addresses both of those issues.
XDocument doc1;
using (IsolatedStorageFileStream myStream = new IsolatedStorageFileStream( "categorie_settings.xml", FileMode.Open, myStore ))
{
doc1 = XDocument.Load( myStream );
}
var root = doc1.Element( "Root" );
var rows = root.Descendants( "Row" );
var lastRow = rows.Last();
lastRow.AddAfterSelf(
new XElement( "Row",
new XElement( "Nome", "Homepage2" ),
new XElement( "Tipo", "News2" ),
new XElement( "URL", "/web/xml2" ),
new XElement( "Err", "Errore2" ) ) );
using (IsolatedStorageFileStream myStream = new IsolatedStorageFileStream( "categorie_settings.xml", FileMode.Create, myStore ))
{
doc1.Save( myStream );
}
There the save is done on a stream that creates the file, thereby overwriting the previous file. Alternatively you could use XDocument.Save( String filename ) which does replace the contents of the file.
来源:https://stackoverflow.com/questions/7957066/add-an-element-to-an-xml-file-in-isolatedstorage