如何阅读特定的XML节点?

时间:2015-09-22 04:29:44

标签: c# xml wpf

我有一个XML文件,其中包含与多个对象相对应的数据。我正在尝试打开XML文件,循环查找特定文件名并读入与之关联的所有值。

问题:

XMLElement的名称和文件名之间永远不匹配。所以“我匹配当前打开的文件名”永远不会被打印出来。

我想要发生什么:因此,当用户在OpenFileDialog中打开“dog.jpg”时,XML文档会被加载,并且应该能够找到XML元素“命名为“使用值dog.jpg并打印”我匹配当前打开的文件名“。

另外,我想知道在获得不同距离值的匹配后如何读取其他相应的值?

我的Open方法中的代码:

string fileName = openFileDialog1.FileName; //file name of a JPEG file opened by a user
XmlDocument doc = new XmlDocument();
doc.Load(@"C:\Users\Desktop\TangramsTool\patterndata.xml");
XmlNodeList nodeList = doc.SelectNodes("/Patterns/Pattern");
foreach (XmlNode node in nodeList)
    {
         string text = node["Name"].InnerText; //or loop through its children as well
         if (text.Equals(fileName))
         {
              Console.WriteLine("I match the currently open file's name: " + text);
         }
         else
         {
              Console.WriteLine("This node's name is : " + text);
         }
    }

我的XML文件:

<Patterns>
     <Pattern> 
         <Name>dog.jpg</Name> 
         <PatternDistancesList>       
              <PatternDistance>278</PatternDistance>
              <PatternDistance>380</PatternDistance>
         </PatternDistancesList> 
     <Pattern/>
     <Pattern> 
          <Name>cat.jpg</Name> 
          <PatternDistancesList>       
              <PatternDistance>278</PatternDistance>
              <PatternDistance>380</PatternDistance>
          </PatternDistancesList> 
     <Pattern/>
</Patterns>

4 个答案:

答案 0 :(得分:1)

openFileDialog1.FileName返回文件的完整路径 使用openFileDialog1.SafeFileName只获取文件名,您将获得所需的结果。字符串不算数学,因为其中一个获取文件名而另一个获取完整路径。使用openFileDialog1.SafeFileName,我相信你会得到一个匹配。

答案 1 :(得分:1)

使用Linq尝试以下方式。答案还包括您的以下查询。

  

另外,我想知道在获得不同距离值的匹配后如何读取其他相应的值?

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            string uploadFilename = "dog.jpg";
            XDocument xdoc = XDocument.Load(@"C:\Users\admin\XMLFile1.xml");

            //// check if the xml file is having node mathcing upload filename name 
            List<XElement> xel = xdoc.Descendants("Name").Where(x => x.Value == uploadFilename).ToList();

            if (xel.Any())
            {
                Console.WriteLine("I match the currently open file's name: " + uploadFilename);

                //// Get list of element list's Ancestors
                //// will return
                ////<Name>dog.jpg</Name>
                ////<PatternDistancesList>
                ////  <PatternDistance>278</PatternDistance>
                ////  <PatternDistance>380</PatternDistance>
                ////</PatternDistancesList>

                //// looop through it
                foreach (XElement item in xel.Ancestors("Pattern").Elements().ToList())
                {

                }

                //// OR get another list
                List<XElement> foundItems = xel.Ancestors("Pattern").Elements().ToList();
            }

        }
    }
}

这是使用控制台应用程序的基本帮助。相应地修改代码。希望能帮助到你。 :)

答案 2 :(得分:1)

从文件对话框中获取文件名:

string fileName = openFileDialog1.SafeFileName;

加载XmlDocument:

XDocument xdoc = XDocument.Load(@"C:\Users\Desktop\TangramsTool\patterndata.xml");

获取匹配的XElements:

var MatchingPatterns=xdoc.Elements("Pattern").Where(o=>o.Element(Name).Value.Trim()==filename).FirstOrDefault();

if(MatchingPatterns!=null)
{
 Console.WriteLine("I match the currently open file's name: " + fileName );
}

您可以像这样获取PatternDistance列表:

   List<XElement> patternDistances= MatchingPatterns.Element("PatternDistancesList").Elements("PatternDistance").ToList();

这可能有帮助!

答案 3 :(得分:0)

<Patterns>
     <Pattern> 
         <Name>dog.jpg</Name> 
         <PatternDistancesList>       
              <PatternDistance>278</PatternDistance>
              <PatternDistance>380</PatternDistance>
         </PatternDistancesList> 
     </Pattern>
     <Pattern> 
          <Name>cat.jpg</Name> 
          <PatternDistancesList>       
              <PatternDistance>278</PatternDistance>
              <PatternDistance>380</PatternDistance>
          </PatternDistancesList> 
     </Pattern>
</Patterns>

string fileName = openFileDialog1.SafeFileName;

确保文件名和节点[“Name”]。InnerText相同