C#中的数组和xml比较 [英] array and xml comparison in c#

查看:92
本文介绍了C#中的数组和xml比较的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以将数组中保存的值与驱动器c上的xml文件进行比较?请帮我.如果可能的话,请指导我如何进行编码.

谢谢. :)

解决方案

假定一个xml:

 <   pre     lang   ="  > 
<   xml      ="  <  名称 > 
<  名称 >  Tarun <  /名称 > 
<  名称 > 美味<  /名称 > 
<  名称 > 美味<  /名称 > 
<  /名称 > 
<  /pre  >  



加载Xml:

 XmlDocument xmlDoc =  XmlDocument();
xmlDoc.Load(" );

// 您的数组在这里!
字符串 [] myArray =  字符串 [ ] {" " 美味"};

// 现在搜索名称:

 foreach (XmlNode myXmlNode  in  xmlDoc.Childnodes中)
{
   for ( int  i =  0 ; i< ; myarray.length; i ++)> 
  {
    如果(myArray [i] == myXmlNode.)
     {
      messagebox(myArray [i]);
     }
  }
} 



如果要直接转到xml中的名称"节点,最好学习XPath.
像这样指定:

 字符串 myXPath = " ; // 它将指向所有名称节点

XmlNodeList myNodesList = xmlDoc.SelectNodes(myXPath); // 选择所有名称"节点 


然后用以下内容替换上面for循环的顶部:

  foreach (XmlNode myXmlNode  in  myNodesList中)


首先,您需要解析XML(或者可以反序列化),然后执行比较.


Is it possible to compare the values saved in an array to the xml file located at drive c? Please help me. If it is possible, please guide me on how to code that.

Thanks. :)

解决方案

Assuming an xml:

<pre lang="xml">
<xml version="1.0">
<Names>
<Name>Tarun</Name>
<Name>Yummy</Name>
<Name>Delicious</Name>
</Names>
</pre>



Load the Xml:

XmlDocument xmlDoc=new XmlDocument();
xmlDoc.Load("C:\MyXml.xml");

//your array here!
String[] myArray=new String[]{"Tarun","Yummy"};

// Now search for names:

foreach(XmlNode myXmlNode in xmlDoc.Childnodes)
{
  for(int i=0;i<myarray.length;i++)>
  {
    if (myArray[i]==myXmlNode.value)
     {
      messagebox(myArray[i]);
     }
  }
}



Well its better to learn XPath if you want to go directly to Name node in the xml.
like specifying in this form:

String myXPath="/Names/Name";   //it will point to all the name nodes

XmlNodeList myNodesList=xmlDoc.SelectNodes(myXPath); //Selects all Name node


then replace top portion of the above for loop with this:

foreach(XmlNode myXmlNode in myNodesList)


First you need to parse XML (or may be deserialize it) and then perform the comparison.


这篇关于C#中的数组和xml比较的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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