我这是把控件动态,写到XML文件中的,我要读取Textbox  Name 属性里面的值<Form>
  <Label Name="lbl1" Top="39" Left="31" Height="23" Width="40" Text="姓名" xmlns="Label">姓名</Label>
  <TextBox Name="txt2" Top="35" Left="89" Height="21" Width="100" Text="" xmlns="TextBox">
  </TextBox>
  <Label Name="lbl3" Top="37" Left="219" Height="23" Width="40" Text="其它" xmlns="Label">其它</Label>
  <TextBox Name="txt4" Top="29" Left="277" Height="21" Width="100" Text="" xmlns="TextBox">
  </TextBox>
</Form>
            XmlDocument xmlDoc = new XmlDocument();  
            xmlDoc.Load("XMLFile1.xml");        
            XmlNodeList xnl = xmlDoc.SelectNodes("//Form/TextBox");  //这里得取节点列表该如何写???
            foreach (XmlNode linkNode in xnl)
            {
                XmlElement xe = (XmlElement)linkNode;
                if (xe.Name == "Name")
                {
                    MessageBox.Show(xe.Attributes["txt2"].Value);
                }
            }
哪位如果有更有效的读取方法,能否方便贴上来

解决方案 »

  1.   

    XmlNodeList xnl = xmlDoc.SelectNodes("//Form/TextBox");  
    但是我这样写 一直得不到值呢!!!这一段不知道如何改了
      

  2.   


     public void XMLTest()
            {
                string Str = "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>";
                Str=Str+"  <Form>";
                Str=Str+"<Label Name=\"lbl1\" Top=\"39\" Left=\"31\" Height=\"23\" Width=\"40\" Text=\"姓名\" xmlns=\"Label\">姓名</Label>";
                Str=Str+" <TextBox Name=\"txt2\" Top=\"35\" Left=\"89\" Height=\"21\" Width=\"100\" Text=\"\" xmlns=\"TextBox\">ddssdsd";
                Str=Str+" </TextBox>";
                Str = Str + " <Label Name=\"lbl3\" Top=\"37\" Left=\"219\" Height=\"23\" Width=\"40\" Text=\"其它\" xmlns=\"Label\">其它</Label>";
                Str=Str+"<TextBox Name=\"txt4\" Top=\"29\" Left=\"277\" Height=\"21\" Width=\"100\" Text=\"\" xmlns=\"TextBox\">";
                Str=Str+"</TextBox>";
                Str=Str+"</Form>";            StringReader Reader = new StringReader(Str);
                XmlDocument xmlDoc = new XmlDocument();
                xmlDoc.Load(Reader);
                XmlNodeList xnl = xmlDoc.SelectNodes("Form");
                foreach (XmlNode linkNode in xnl[0].ChildNodes)//这里获取的是所有的节点
                {
                    if (linkNode.Name.ToString() == "TextBox" && linkNode.Attributes["Name"].Value.ToString() == "txt2")
                    {
                        MessageBox.Show(linkNode.InnerText);//显示Name为txt2的TextBox的节点的值
                    }
                }        }
      

  3.   


    XmlNodeList xnl = xmlDoc.SelectNodes("Form/TextBox");  //这里得取节点列表该如何写???
    foreach (XmlNode linkNode in xnl)
    {
       if(XmlNode.innerHTML=="")//得到TextBox
       {
           if(XmlNode.Attributes["Name"].Value == "txt2")
           {
           }
       }
    }
      

  4.   

    sorry 弄错了,看楼主代码以为是得到Name为txt2的TextBox得到Textbox Name 属性里面的值
    XmlNodeList xnl = xmlDoc.SelectNodes("Form/TextBox");  //这里得取节点列表该如何写???
    foreach (XmlNode linkNode in xnl)
    {
       MessageBox.Show(XmlNode.Attributes["Name"].Value.ToString());
    }
      

  5.   

      string xml=@"<?xml version=""1.0"" encoding=""utf-8"" ?>
    <Form xmlns:xsd=""http://www.w3.org/FormSample""> <TextBox Name=""txt1"" Top=""364"" Left=""650"" Height=""21"" Width=""100"" Text="" xmlns=""uri:http://www.w3.org/Controls/TextBox""></TextBox></Form>";
            XmlDocument xmlDoc = new XmlDocument();
            xmlDoc.LoadXml(xml);
            XmlNodeList xnl = xmlDoc.SelectNodes("Form/TextBox");
            foreach (XmlNode linkNode in xnl)
            {
                XmlElement xe = (XmlElement)linkNode;//将子节点类型转换为XmlElement类型
                Response.Write(xe.Attributes["Name"].Value);
            }
      

  6.   

    <TextBox>这里存放的就是文本框的值</TextBox>
    在xmlnode中是。xmlnode.innerText
      

  7.   

    貌似要的就是属性值。xpath语句有问题
    修改下
     XmlDocument xmlDoc = new XmlDocument();  
                xmlDoc.Load("XMLFile1.xml");        
                XmlNodeList xnl = xmlDoc.SelectNodes("//TextBox");  //这里得取节点列表该如何写???
                foreach (XmlNode linkNode in xnl)
                {
                   MessageBox.Show(linkNode.Attributes["txt2"].Value.toString());
                }
      

  8.   


    public void XMLTest()
            {
                string Str = "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>";
                Str=Str+"  <Form>";
                Str=Str+"<Label Name=\"lbl1\" Top=\"39\" Left=\"31\" Height=\"23\" Width=\"40\" Text=\"姓名\" xmlns=\"Label\">姓名</Label>";
                Str = Str + " <TextBox Name=\"txt2\" Top=\"35\" Left=\"89\" Height=\"21\" Width=\"100\" Text=\"\" xmlns=\"TextBox\">这是文本框的值</TextBox>";//下面的messagebox就弹出了该值
                Str = Str + " <Label Name=\"lbl3\" Top=\"37\" Left=\"219\" Height=\"23\" Width=\"40\" Text=\"其它\" xmlns=\"Label\">其它</Label>";
                Str = Str + "<TextBox Name=\"txt4\" Top=\"29\" Left=\"277\" Height=\"21\" Width=\"100\" Text=\"\" xmlns=\"TextBox\">TextBoxValue1</TextBox>";
                Str=Str+"</Form>";            StringReader Reader = new StringReader(Str);
                XmlDocument xmlDoc = new XmlDocument();
                xmlDoc.Load(Reader);
                XmlNodeList xnl = xmlDoc.SelectNodes("Form");  //这里得取节点列表该如何写???
                foreach (XmlNode linkNode in xnl[0].ChildNodes)
                {
                    if (linkNode.Name.ToString() == "TextBox" && linkNode.Attributes["Name"].Value.ToString() == "txt2")
                    {
                        MessageBox.Show(linkNode.InnerText);//弹出内容为:这是文本框的值
                    }
                }        }
      

  9.   

    XmlDocument xmlDoc = new XmlDocument();  
    xmlDoc.Load("XMLFile1.xml");        
    XmlElement xe 
        = (XmlElement)xmlDoc.SelectNode("Form/TextBox[@Name=\"txt2\"]"); 
    MessageBox.Show(xe.Attributes["Text"].Value);