问题:cs代码的while循环部分有两个if语句,如果加入reader.Name的比较条件,if语句Console.WriteLine就没有输出。如果去掉reader.Name的比较条件或只保留这个条件,则Console.WriteLine可输出。让人纳闷的是,其中的reader.Value可用于比较,而reader.Name用于此处就不行,这是什么原因。此贴悬赏50分求解,为将分数集中给解决问题者,首解此贴(包括指出问题所在)的回贴给全部悬赏分。
test.xml:
============
<?xml version="1.0" encoding="gb2312" ?>
<root>
<sgname>中</sgname>
<sgname>华</sgname>
<sgname>人</sgname>
<sgname>民</sgname>
<dbname>中华</dbname>
<dbname>人民</dbname>
<dbname>华夏</dbname>
<dbname>民族</dbname>
</root>test.cs:
============
using System;
using System.Xml;class test {    static void Main() {
        string strTest = "中国";
        if(strTest.Length<2 || strTest.Length>4){Console.WriteLine("strTest长度不符合要求!");return;}
        bool isSgMatch = false;
        bool isDbMatch = false;        string strSingleName = strTest.Substring(0,1);
        string strDoubleName = strTest.Substring(0,2);
        string xmlPath = "test.xml";        XmlTextReader reader = null;        try {
            reader = new XmlTextReader(xmlPath);
            reader.WhitespaceHandling = WhitespaceHandling.None;            while(reader.Read())
            {
                if(isSgMatch==false && reader.Value==strSingleName && reader.Name=="sgname")
                {
                    Console.WriteLine(reader.Value);
                    isSgMatch=true;
                }
                if(isDbMatch==false && reader.Value==strDoubleName && reader.Name=="dbname")
                {
                    Console.WriteLine(reader.Value);
                    isDbMatch=true;
                }
            }
       }
       finally {
           if (reader!=null) reader.Close();
       }
       Console.WriteLine(isSgMatch);
       Console.WriteLine(isDbMatch);
    }
}

解决方案 »

  1.   

    http://msdn.microsoft.com/library/chs/default.asp?url=/library/CHS/cpref/html/frlrfsystemxmlxmltextreaderclassctortopic.asp
    你加这个试试,reader.Name的值取的到吗?
     while (reader.Read()) {
               switch (reader.NodeType) {
                                
                 case XmlNodeType.Text:
                   Console.Write(("{0} {1}", reader.Value,reader.Name);
                   break;
         }       
            }           
         }
      

  2.   

    你的错误在于:
    <sgname>中</sgname>
    被看成2个元素:
    也就是说:sgname节点类型Element,它的Name="sgname"
    中看成是Text类型的节点。它的Name=""
    所以,永远不会存在 reader.Value==strSingleName && reader.Name=="sgname"为true的情况
    你可以这样测试
    while(reader.Read())
    {        
    Console.WriteLine("===============");
    Console.WriteLine(reader.Value + "==" + (reader.Value) +">"+ ( reader.Name));    if(isSgMatch==false && reader.Value==strSingleName && reader.Name=="sgname")
        {
            Console.WriteLine(reader.Value);
            isSgMatch=true;
        }
        if(isDbMatch==false && reader.Value==strDoubleName && reader.Name=="dbname")
        {
            Console.WriteLine(reader.Value);
            isDbMatch=true;
        }
    }
      

  3.   

    net_lover说到点子上了,偶也明白了其中的道理,代码改得不好,总之得到正确的结果了,稍后再完善,同时也请高手不另赐教,奉献一段高质量的代码。using System;
    using System.Xml;class test {    static void Main() {
            string strTest = "中国";
            if(strTest.Length<2 || strTest.Length>4){Console.WriteLine("strTest长度不符合要求!");return;}
            bool isSgMatch = false;
            bool isDbMatch = false;        string strSingleName = strTest.Substring(0,1);
            string strDoubleName = strTest.Substring(0,2);
            string xmlPath = "test2.xml";        XmlTextReader reader = null;        try {
                reader = new XmlTextReader(xmlPath);
                reader.WhitespaceHandling = WhitespaceHandling.None;
                string rn=null;//定义rn,用于保存Element名称
                while(reader.Read())
                {
                    //Console.WriteLine(reader.Value + "==" + (reader.Value) +">"+ ( reader.Name));                //考虑到本XML文档很规范,Element节点类型之后是Text类型的节点
                    //鉴于这个原因,想到一个折中的办法
                    //while循环中,首先switch判断并读取和保存reader.Name,即Element名称
                    //接着,在下一次循环中,才执行if语句中的代码块,从而实现目标
                    switch (reader.Name) {
                        case "sgname": rn="sgname";break;
                        case "dbname": rn="dbname";break;
                    }
                    if(isSgMatch==false && reader.Value==strSingleName && rn=="sgname")
                    {
                        Console.WriteLine(reader.Value);
                        isSgMatch=true;
                    }
                    if(isDbMatch==false && reader.Value==strDoubleName && rn=="dbname")
                    {
                        Console.WriteLine(reader.Value);
                        isDbMatch=true;
                    }
                }
           }
           finally {
               if (reader!=null) reader.Close();//关闭指针
           }
           Console.WriteLine(isSgMatch);
           Console.WriteLine(isDbMatch);
        }
    }