gpt4 book ai didi

c# - 删除 XML 元素节点

转载 作者:数据小太阳 更新时间:2023-10-29 02:53:38 27 4
gpt4 key购买 nike

我想使用 C# 删除本地 xml 文件中的 Element Node 及其所有 Child Elements

这是我的 xml 文件:

<?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>

我想删除一个 id 属性为 4 的鸡尾酒元素,它存储在一个变量中。我如何告诉我的应用它只需要删除 id 为 4 的鸡尾酒元素?XmlNode.RemoveChild 将不起作用,因为它在 Windows Phone 8 中不受支持/不可用。我写了下面的代码,但我不知道在哪里写我想删除的元素的 id。

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();

或者使用 XPATH

xDoc.XPathSelectElement("//cocktail[@id='4']").Remove();

关于c# - 删除 XML 元素节点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21892155/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com