如何从 c# 中的 xml 文件读取/写入节点和子节点,也许使用 xpath? [英] How to read/write nodes and child nodes from xml file in c# maybe using xpath?

查看:24
本文介绍了如何从 c# 中的 xml 文件读取/写入节点和子节点,也许使用 xpath?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

也许有人可以帮助我.我需要两种方法.

Maybe somebody could help me. I need a two methods.

第一个应该打开一个 xml 文件并获取具有给定参数的所有节点,即:

The first should open a xml file and get all nodes with the given parameter ie.:

XML 文件(file.xml):

XML file (file.xml):

<Menu id="1" Name="myMenu">
  <MenuItem Header="Header 1" Name="header1" />
  <MenuItem Header="Header 2" Name="header2">
    <MenuItem Header="subHeader 2.1" Name="header2_1">
      <MenuItem Header="subsubHeader 2.1.1" Name="header2_1_1" />
    </MenuItem>
  </MenuItem>
  <MenuItem Header="Header 3" Name="header3" />
</Menu>

所以,现在我需要使用这样的方法从 xml 中获取值:

So, now I need to get the values from the xml with a method like this:

public static List<string, string>ReadXML(string filename, string node, string[] attributes, bool searchSubNodes);

调用方法示例:ReadXMLValues("file.xml", "MenuItem", new string[] {"Header", "Name"}, true);

这将返回一个包含两个字符串的列表,例如:

and this would return a list of two strings like:

"Header 1", "header1"
"Header 2", "header2"
"subHeader 2.1", "header2_1" <-- this should be in the list only if searchSubNodes is enabled!
"subsubHeader 2.1.1", "header2_1_1" <-- the same for this one!!!
"Header 3", "header3"

这是阅读部分,现在是写作部分:

THIS was the reading part and now the writting part:

filename 与上面的 file.xml 相同.public static void WriteXML(string filename, string node, List attributes);

filename is the same like above file.xml. public static void WriteXML(string filename, string node, List attributes);

现在让我们说 file.xml 有这样的空头属性:

now lets say the file.xml has empty header attributes like this:

<Menu id="1" Name="myMenu">
  <MenuItem Header="" Name="header1" />
  <MenuItem Header="" Name="header2">

我需要将值放入标题中,最终结果应如下所示:

And I need to put values into the headers, the finall result should look like this:

   <Menu id="1" Name="myMenu">
      <MenuItem Header="Header 1" Name="header1" />
      <MenuItem Header="Header 2" Name="header2">

这样的事情可能吗???C# 大师和其他知道如何做到这一点的人请帮助我!我不知道该怎么做.

Is something like this possible??? C# gurus and other people who knows how to do this PLEASE PLEASE help me! I don't know how to do it.

最好的问候!

推荐答案

要使用 XPath 选择具有给定节点名称和两个给定属性的所有节点,请使用以下 XPath 表达式(我不知道是否有更好的方法来执行此操作,但此方法有效):

To use XPath to select all nodes that have a given node name AND two given attributes use the following XPath expression (I don't know if there are better ways to do it but this works):

//NodeName[@FirstAttributeName][@SecondAttributeName]

在代码中,您可以这样做(对于您示例中的 MenuItems).我更新了代码,使您放在文档中的命名空间以前缀x"为名.此外,XPath 表达式已更新为在节点之前使用该前缀.

In code you could do it like that (for the MenuItems from your example). I updated the code to make the namespace you put in your document known under the prefix "x". Also the XPath expression has been updated to use that prefix before the node.

XPathDocument doc = new XPathDocument("data2.xml");
XPathNavigator nav = doc.CreateNavigator();
XmlNamespaceManager mgr = new XmlNamespaceManager(nav.NameTable);
mgr.AddNamespace("default", "http://schemas.microsoft.com/winfx/2006/xaml/presentation");
XPathNodeIterator iterator = nav.Select("//default:MenuItem[@Header][@Name]", mgr);

while (iterator.MoveNext())
{
    Console.Write(iterator.Current.GetAttribute("Header", ""));
    Console.Write(iterator.Current.GetAttribute("Name", ""));
    Console.Write(Environment.NewLine);
}

======

你必须使用 XPath 吗?您可以为 Menu/MenuItem 创建映射到您的 XML 的类,如下所示:

Do you have to use XPath? You could just create classes for Menu/MenuItem that map to your XML like this:

public class Menu
{
    public int Id { get; set; }
    public string Name { get; set; }
    public List<MenuItem> SubItems { get; set; }
}

public class MenuItem
{
    public string Header { get; set; }
    public string Name { get; set; }
    public List<MenuItem> SubItems { get; set; }
}

然后有一个 XMLSerializer为您完成工作(它可以同时完成阅读和写作).之后您可以对内存列表应用过滤(例如使用 Linq-To-Objects).

And then have an XMLSerializer do the work for you (it can do both, reading and writing). You could afterwards apply filtering to the in-memory lists (e.g. using Linq-To-Objects).

如果您想使用 XPath 执行此操作,此 Codeproject article 可能是很有帮助.

If you want to do it using XPath this Codeproject article might be helpful.

这篇关于如何从 c# 中的 xml 文件读取/写入节点和子节点,也许使用 xpath?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆