问题: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);
}
}
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);
}
}
解决方案 »
- 解析XML文件显示到Treeview中的代码
- 插入自定义控件后,自定义控件的背景颜色变了。
- WinForm做好的EXE文件如何加个图标?
- 如何动态增加类数组的个数
- 按钮焦点问题
- C#连接mysql的问题
- custDA.InsertCommand.Parameters.Add("@CustomerID", OdbcType.Char, 5, "CustomerID");
- 怎样让程序独立运行?
- 请大家帮忙讨论,如何实现web应用程序和windows应用程序(后台)间进行通信!参与给分
- 服用禁药 西班牙被取消欧洲冠军!
- 一个简单问题:在WINFORM里怎样把字符串转为URL编码?
- 程序布署到用户机子上后,FreeTextBox的问题
你加这个试试,reader.Name的值取的到吗?
while (reader.Read()) {
switch (reader.NodeType) {
case XmlNodeType.Text:
Console.Write(("{0} {1}", reader.Value,reader.Name);
break;
}
}
}
<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;
}
}
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);
}
}