当前位置:编程学习 > C#/ASP.NET >>

取不到XML文档的值,急死了,大侠们请进

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                XmlDocument xmdoc = new XmlDocument();
                xmdoc.Load("H:\\ConsoleApplication1\\ConsoleApplication1\\test.xml");
                int num = xmdoc.DocumentElement.ChildNodes.Count;
                Console.WriteLine(num);
                XmlNode nod = xmdoc.SelectSingleNode("employee/易做图");
                string notevalue = nod.Value;
                if (notevalue == null)
                {
                    Console.WriteLine("no value");
                }
                else
                {
                    Console.WriteLine(notevalue);
                }
            }
            catch (FileNotFoundException e)
            {
                string exp = e.FileName;
                Console.WriteLine( exp);
            }
            //finally
            //{
            //    Console.WriteLine("successful");
            //}
             
        }
    }
}

为什么nod总是取不到值啊,xmdoc.SelectSingleNode("employee/易做图")这个语句有错吗?
以下是XML文件
<?xml version="1.0" encoding="utf-8" ?> 
<employee>
  <name>xiaohong</name>
  <易做图>female</易做图>
    <dept>IT</dept>
</employee>
--------------------编程问答-------------------- xmdoc.SelectSingleNode("/employee/易做图") --------------------编程问答-------------------- xpath命名空间引用了吗
不行就用xmdoc.GetElementsByTagName("易做图") --------------------编程问答-------------------- 利用一个winform的例子吧。:)

我的XML例子的代码如下:
<?xml version="1.0" encoding="utf-8" ?>
<carlot>
  <car>
    <make>234</make>
    <model>324</model>
    <elapsedMiles>fdsfdsf</elapsedMiles>
  </car>
  <car>
    <make>23434</make>
    <model>3244</model>
    <elapsedMiles>dsfds</elapsedMiles>
  </car>
  
  
</carlot>


我的C#代码:
            System.IO.StreamReader sr = new System.IO.StreamReader(@"cars.xml");

            System.Xml.XmlTextReader xr = new System.Xml.XmlTextReader(sr);

            System.Xml.XmlDocument carlotDoc = new System.Xml.XmlDocument();

            carlotDoc.Load(xr);

            label1.Text = carlotDoc.InnerText;

            label2.Text ="First Child Node:"+ carlotDoc.FirstChild.InnerText;

            label3.Text = "Second Child Node:" + carlotDoc.FirstChild.NextSibling.InnerText;

            System.Xml.XmlNode carlot = carlotDoc.FirstChild.NextSibling;

            label4.Text = "Now we have a reference to 'carlot':" + carlot.InnerText;

            label5.Text = "First chlid of carlot:" + carlot.FirstChild.InnerText;

            label6.Text = "First child of first child of carlot;" + carlot.FirstChild.FirstChild.InnerText;

            System.Xml.XmlNodeList carLotItems = carlotDoc.SelectNodes("carlot/car");

            System.Xml.XmlNode make = carLotItems.Item(0).SelectSingleNode("make");

            label7.Text ="make.innertext:"+ make.InnerText;


不好意思。里面拖了7个label控件。执行一下,看结果,可以对你作为参考。

--------------------编程问答-------------------- string notevalue = nod.InnerText;
补充:.NET技术 ,  C#
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,