-1

我有一个 XML 文件,我想从以下 XML中检索brandname 时间。brandcode001

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

这是我的代码;我需要在这里指定品牌名称:

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);
            }
4

4 回答 4

3

试试这种方式...

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();
        }
    }
}
于 2012-07-30T05:11:13.387 回答
1

试试这个XPath//Brandname[../Brandcode[text()='001']]

使用XmlDocument.SelectSingleNode

var document = new XmlDocument();
var Brandcode = "001";
var xpath = String.Format(@"//Brandname[../Brandcode[text()='{0}']]", 
                          Brandcode);
var Brandname = document.SelectSingleNode(xpath).InnerText;

并使用XDocument.XPathSelectElement

var document = XDocument.Load("fileName");
var name = document.XPathSelectElement(xpath).Value;
于 2012-07-30T05:03:51.177 回答
0

您可以使用 Xml.Linq 命名空间和 XDocument 类来加载并从 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;
于 2012-07-30T05:05:32.737 回答
0

将品牌名称定义为字符串并将 null 分配给它,然后在您想要的任何地方使用品牌名称....

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();
        }
    }
}
于 2012-07-30T08:45:05.313 回答