[英]How do I retrieve data that meets a specific condition in an XML file?
I have an XML file, and I want to retrieve the brandname
when brandcode
is 001
from the following XML. 我有一个XML文件,并且我想从以下XML中检索
brandcode
为001
时的brandname
。
<Root>
- <data>
<Companycode>TF</Companycode>
<Productcode>00001</Productcode>
<Productname>VPU</Productname>
<Brandcode>001</Brandcode>
<Brandname>DB</Brandname>
</data>
- <data>
<Companycode>TF</Companycode>
<Productcode>00002</Productcode>
<Productname>SENDERCARD</Productname>
<Brandcode>002</Brandcode>
<Brandname>LINSN</Brandname>
</data>
</Root>
This is my code; 这是我的代码; I need to assign Brand Name here:
我需要在此处分配品牌名称:
XmlTextReader textReader = new XmlTextReader(@"codedata.xml");
textReader.Read();
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(textReader);
XmlNodeList BCode = xmlDoc.GetElementsByTagName("Brandcode");
XmlNodeList BName = xmlDoc.GetElementsByTagName("Brandname");
for (int i = 0; i < BCode.Count; i++)
{
if (BCode[i].InnerText =="001")
{
string brandname = BName[i].InnerText;
}
//Console.WriteLine(BName[i].InnerText);
}
Try In this way... 以这种方式尝试...
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
namespace XmlReading
{
class Program
{
static void Main(string[] args)
{
//Create an instance of the XmlTextReader and call Read method to read the file
XmlTextReader textReader = new XmlTextReader("D:\\myxml.xml");
textReader.Read();
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(textReader);
XmlNodeList BCode = xmlDoc.GetElementsByTagName("Brandcode");
XmlNodeList BName = xmlDoc.GetElementsByTagName("Brandname");
for (int i = 0; i < BCode.Count; i++)
{
if (BCode[i].InnerText == "001")
Console.WriteLine(BName[i].InnerText);
}
Console.ReadLine();
}
}
}
Try this XPath
: //Brandname[../Brandcode[text()='001']]
试试这个
XPath
: //Brandname[../Brandcode[text()='001']]
Using XmlDocument.SelectSingleNode
: 使用
XmlDocument.SelectSingleNode
:
var document = new XmlDocument();
var Brandcode = "001";
var xpath = String.Format(@"//Brandname[../Brandcode[text()='{0}']]",
Brandcode);
var Brandname = document.SelectSingleNode(xpath).InnerText;
and using XDocument.XPathSelectElement
: 并使用
XDocument.XPathSelectElement
:
var document = XDocument.Load("fileName");
var name = document.XPathSelectElement(xpath).Value;
YOu can use Xml.Linq namespace, with XDocument class to load and select the exact element from the xml file: 您可以使用带有XDocument类的Xml.Linq命名空间来加载并从xml文件中选择确切的元素:
XDocument doc = XDocument.Load(@"filePath");
var query = doc.Descendants("Root").Where(w => w.Element("Brandcode").Value == "001").Select(s => new { s.Element("Brandname").Value }).FirstOrDefault();
string brandname = query.Value;
Define the brandname as string and assign null to it and then use the brand name where ever you want.... 将品牌名称定义为字符串,并为其分配空值,然后在任何需要的地方使用品牌名称。
namespace XmlReading
{
class Program
{
static void Main(string[] args)
{
//Create an instance of the XmlTextReader and call Read method to read the file
XmlTextReader textReader = new XmlTextReader("D:\\myxml.xml");
textReader.Read();
string brandname = null;
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(textReader);
XmlNodeList BCode = xmlDoc.GetElementsByTagName("Brandcode");
XmlNodeList BName = xmlDoc.GetElementsByTagName("Brandname");
for (int i = 0; i < BCode.Count; i++)
{
if (BCode[i].InnerText == "001")
{
brandname = BName[i].InnerText;
}
}
Console.WriteLine(brandname);
Console.ReadLine();
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.