0

私のアプリケーションでは、提供された 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>

私が興味を持っているのは、マシン/名前/を取得する方法を知ることだけです/これまでのところ、特定のタグを選択する方法がわかりません。マシン タグ内の各名前を選択する方法は誰でも知っています。除外する 300 MB のファイルがあります。

私のアプローチは、 Machine タグ内の各 Name を取得して文字列に格納し、後でその文字列を使用して構造を作成することです。しかし、私は行き詰まっています助けてください...

これまでの私のソースコード:

//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; 

ありがとう、HRG

4

2 に答える 2

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して入力をストリーミングする必要がありますが、それを操作するためにXElementfrom eachMachine要素を作成できます。(これを行う方法については、ネット上にさまざまなページがあります。これには、このページも含まれます。コードは、私が書く方法とはまったく異なりますが、一般的な考え方はそこにあります。)

于 2012-06-21T13:27:59.357 に答える
0

それらを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");
于 2012-06-22T09:09:37.030 に答える