删除XML元素节点 [英] Deleting XML element nodes
问题描述
我要删除元素节点
及其所有子元素
通过使用C#中的本地XML文件
这是我的XML文件:
< ;? XML版本=1.0编码=UTF-8>?;
<数据>
<鸡尾酒的名字=43快乐主义ID =14>
<名称> 43&享乐主义LT; /名称>
<&ID GT; 14 LT; / ID>
< /鸡尾酒GT;
<鸡尾酒的名字=B-52ID =4>
<名称> B-52< /名称>
<&ID GT; 4℃; / ID>
< /鸡尾酒GT;
< /数据>
我想在那里的ID属性是4,其被存储在一个变量来删除一个鸡尾酒元。
我怎么能告诉我的应用程序,它只有删除的鸡尾酒元素,其中id是4?
XmlNode.RemoveChild
将无法工作,因为它没有在Windows Phone的8
/支持可我写了下面的代码,但我被困。在哪里写我要删除的元素的ID
使用系统;
:使用System.IO;
使用System.IO.IsolatedStorage;
使用System.Collections.Generic;
使用System.Linq的;使用System.Net
;使用System.Windows
;使用System.Windows.Resources
;使用System.Windows.Controls的
;
使用System.Windows.Navigation;使用Microsoft.Phone.Controls
;
使用Microsoft.Phone.Shell;使用XmlLocalDelete1.Resources
;
使用的System.Xml;
使用System.Xml.Linq的;
使用System.Text;
命名空间XmlLocalDelete1
{
公共部分类的MainPage:的PhoneApplicationPage
{
//构造
公众的MainPage()
{
的InitializeComponent();
//示例代码本地化应用程序任务栏
// BuildLocalizedApplicationBar();
}
私人字符串ID =4;
保护覆盖无效的OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs E)
{
试
{
tb1.Text =;
//使用复制XML文件到独立存储
(IsolatedStorageFile文件= IsolatedStorageFile.GetUserStoreForApplication())
{
如果(!file.FileExists(bar.xml) )
{
StreamResourceInfo sr_en = Application.GetResourceStream(新的URI(Resources\\bar.xml,UriKind.Relative));使用
(BinaryReader br_en =新BinaryReader(sr_en.Stream))
{
字节[]数据= br_en.ReadBytes((INT)sr_en.Stream.Length);
//写入文件。使用
(BW的BinaryWriter =新的BinaryWriter(file.CreateFile(bar.xml)))
{
bw.Write(数据);
bw.Close();
}
}
}
//与文件中使用工作在isolatedstorage
(IsolatedStorageFileStream流=新IsolatedStorageFileStream(bar.xml的FileMode。打开,文件))
{
的XDocument DOC = XDocument.Load(流LoadOptions.None);
//删除节点
的XElement deleteThis = doc.Element(鸡尾酒);
deleteThis.Remove();
}
//写剩余的Xml使用到文本块
(IsolatedStorageFileStream流=新IsolatedStorageFileStream(bar.xml,FileMode.Open,文件))
{
//加载XML文件
的XmlReader读卡器= XmlReader.Create(流);
,而(reader.Read())
{
开关(reader.NodeType)
{
情况下XmlNodeType.Element://的Het knooppunt是EEN元素。
//tb1.Text = tb1.Text +< + reader.Name;
,而(reader.MoveToNextAttribute())// Attributen lezen。
tb1.Text = tb1.Text ++ reader.Name +='+ reader.Value +';
//tb1.Text = tb1.Text +>中;
中断;
情况下XmlNodeType.Text://德tekst在麋鹿元素weergeven。
//tb1.Text = tb1.Text + reader.Value +\r\\\
Console.WriteLine(reader.Value);
中断;
情况下XmlNodeType.EndElement://的Het einde面包车HET元素weergeven。
Console.Write(&下; /+ reader.Name);
Console.WriteLine(>中);
中断;
}
}
reader.Close();
}
}
}
赶上(例外myExc)
{
Console.WriteLine(myExc.Message);
}
}
}
}
我想删除鸡尾酒元素,其中的id属性为4,
块引用>
XDOC VAR = XDocument.Load(文件名); //或XDocument.Load(流);
xDoc.Descendants(鸡尾酒)第一(C =方式> c.Attribute(ID)值==4)。remove()方法;
串newXml = xDoc.ToString();
或使用XPATH
xDoc.XPathSelectElement(//鸡尾酒[@ ID =4]),删除();
I want to delete an
Element Node
and all itsChild Elements
in a local xml-file by using C#.This is my xml-file:
<?xml version="1.0" encoding="UTF-8"?> <data> <cocktail name="43 Hedonism" id="14"> <name>43 Hedonism</name> <id>14</id> </cocktail> <cocktail name="B-52" id="4"> <name>B-52</name> <id>4</id> </cocktail> </data>
I want to remove a cocktail Element where the id-attribute is 4, which is stored in a variable. How can I tell my app that it only has to delete the cocktail Element where the id is 4?
XmlNode.RemoveChild
will not work since it isn't supported/available in Windows Phone 8. I wrote the following code but I'm stuck on where to write the id of the element I want to remove.using System; using System.IO; using System.IO.IsolatedStorage; using System.Collections.Generic; using System.Linq; using System.Net; using System.Windows; using System.Windows.Resources; using System.Windows.Controls; using System.Windows.Navigation; using Microsoft.Phone.Controls; using Microsoft.Phone.Shell; using XmlLocalDelete1.Resources; using System.Xml; using System.Xml.Linq; using System.Text; namespace XmlLocalDelete1 { public partial class MainPage : PhoneApplicationPage { // Constructor public MainPage() { InitializeComponent(); // Sample code to localize the ApplicationBar //BuildLocalizedApplicationBar(); } private string id = "4"; protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e) { try { tb1.Text = ""; // copy the xml file to isolated storage using (IsolatedStorageFile file = IsolatedStorageFile.GetUserStoreForApplication()) { if (!file.FileExists("bar.xml")) { StreamResourceInfo sr_en = Application.GetResourceStream(new Uri("Resources\\bar.xml", UriKind.Relative)); using (BinaryReader br_en = new BinaryReader(sr_en.Stream)) { byte[] data = br_en.ReadBytes((int)sr_en.Stream.Length); //Write the file. using (BinaryWriter bw = new BinaryWriter(file.CreateFile("bar.xml"))) { bw.Write(data); bw.Close(); } } } // work with file at isolatedstorage using (IsolatedStorageFileStream stream = new IsolatedStorageFileStream("bar.xml", FileMode.Open, file)) { XDocument doc = XDocument.Load(stream, LoadOptions.None); // delete node XElement deleteThis = doc.Element("cocktail"); deleteThis.Remove(); } // Write remaining Xml to textblock using (IsolatedStorageFileStream stream = new IsolatedStorageFileStream("bar.xml", FileMode.Open, file)) { // Load the XML file XmlReader reader = XmlReader.Create(stream); while (reader.Read()) { switch (reader.NodeType) { case XmlNodeType.Element: // Het knooppunt is een element. //tb1.Text = tb1.Text + "<" + reader.Name; while (reader.MoveToNextAttribute()) // Attributen lezen. tb1.Text = tb1.Text + " " + reader.Name + "='" + reader.Value + "'"; //tb1.Text = tb1.Text + ">"; break; case XmlNodeType.Text: //De tekst in elk element weergeven. //tb1.Text = tb1.Text + reader.Value + "\r\n"; Console.WriteLine(reader.Value); break; case XmlNodeType.EndElement: //Het einde van het element weergeven. Console.Write("</" + reader.Name); Console.WriteLine(">"); break; } } reader.Close(); } } } catch (Exception myExc) { Console.WriteLine(myExc.Message); } } } }
解决方案I want to remove a cocktail Element where the id-attribute is 4,
var xDoc = XDocument.Load(filename); //or XDocument.Load(stream); xDoc.Descendants("cocktail").First(c => c.Attribute("id").Value == "4").Remove(); string newXml = xDoc.ToString();
OR using XPATH
xDoc.XPathSelectElement("//cocktail[@id='4']").Remove();
这篇关于删除XML元素节点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!