【问题标题】:How to pick specific tag values in XML data如何在 XML 数据中选择特定的标记值
【发布时间】:2012-06-21 13:23:43
【问题描述】:

在我的应用程序中,我需要使用提供的 xml 文件在本地计算机上创建一个文件夹结构,例如 "*C:\Laptop1\folder\" 、 "C:\Laptop2\folder*" .现在 XML 文件具有我所追求的文件名。

我的xml代码:

<?xml version="1.0" encoding="utf-8" ?>
<Proj>
<MachineIP>
<Machine>
<Name>Laptop 1</Name>
<Path>C:\ZipFiles\Laptop1\folder\</Path>
</Machine>
<Machine>
<Name>Laptop 2</Name>
<Path>C:\ZipFiles\Laptop2\folder\</Path>
</Machine>
<Machine>
<Name>Laptop 3</Name>
<Path>C:\ZipFiles\Laptop2\folder\</Path>
</Machine>
<Machine>
<Name>Laptop 4</Name>
<Path>C:\ZipFiles\Laptop2\folder\</Path>
</Machine>
<Machine>
<Name>Laptop 5</Name>
<Path>C:\ZipFiles\Laptop2\folder\</Path>
</Machine>
<Machine>
<Name>Laptop 6</Name>
<Path>C:\ZipFiles\Laptop2\folder\</Path>
</Machine>
</MachineIP>
</Proj>

我感兴趣的只是知道如何获取机器/名称/ 到目前为止,我不知道如何选择特定的标签。任何人都知道如何选择机器标签中的每个名称。我有一个 300mb 的文件要过滤掉。

我的方法是获取机器标签中的每个名称并将其存储在一个字符串中,然后使用该字符串创建结构。但是我卡住了,请帮助...

到目前为止我的源代码:

//doc created
XmlDocument doc = new XmlDocument();


//loading file:
filePath = System.IO.Directory.GetCurrentDirectory();
filePath = System.IO.Path.Combine(filePath + "\\", "MyConfig.xml");
try
{
     doc.Load(filePath);
}
catch (Exception ex)
{
    MessageBox.Show("Config File Missing: " + ex.Message, "Config File Error",
    MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
    Application.Exit();
}

//fetch data:
String[] MachineName = XMLData("PROJ/MachineIP/Machine", "Name");
String[] MachinePath = XMLData("PROJ/MachineIP/Machine", "Path");

//function XMLData():

string[] temp;
XmlNodeList nodeList = doc.SelectNodes(MainNode);
int i = 0;
temp = new string[nodeList.Count];
foreach (XmlNode node in nodeList)
{
     temp.SetValue(node.SelectSingleNode(SubNode).InnerText, i);
     i++;
}
return temp; 

谢谢, 心电图

【问题讨论】:

  • 到目前为止,我正在使用 XMLDocument,但我发现很难获取每个标签,有很多...

标签: xml c#-4.0


【解决方案1】:

如果你有足够的内存一次性加载整个文件,我会使用 LINQ to XML:

var document = XDocument.Load("file.xml");
var names = document.Root
                    .Element("MachineIP")
                    .Elements("Machine")
                    .Elements("Name")
                    .Select(x => (string) x)
                    .ToList();

如果您没有有足够的内存,则需要使用 XmlReader 流式传输输入 - 尽管您可以从每个 Machine 元素创建一个 XElement 才能工作接着就,随即。 (网上有很多关于如何做到这一点的页面,包括this one。代码不是我要写的,但大致的想法就在那里。)

【讨论】:

  • 谢谢乔恩,我会通过你传递的链接,有没有办法使用 XMLDocument 和 XmlNodeList 来做到这一点???
  • @hrg:当然——但是 LINQ to XML 让 IMO 的生活变得更简单。为什么要使用XmlDocument
【解决方案2】:

我可以将它们读入 2 个数组...这是我的代码如下...

//doc created
XmlDocument doc = new XmlDocument();
//loading file:
filePath = System.IO.Directory.GetCurrentDirectory();
filePath = System.IO.Path.Combine(filePath + "\\", "MyConfig.xml");
try
{
     doc.Load(filePath);
}
catch (Exception ex)
{
    MessageBox.Show("Config File Missing: " + ex.Message, "Config File Error",
    MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
    Application.Exit();
}

//fetch data:
String[] MachineName = XMLData("PROJ/MachineIP/Machine", "Name");
String[] MachinePath = XMLData("PROJ/MachineIP/Machine", "Path");

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-05-24
    • 1970-01-01
    • 1970-01-01
    • 2011-08-13
    • 1970-01-01
    • 2011-06-02
    • 2023-03-14
    • 2016-11-18
    相关资源
    最近更新 更多