解决了XMLTextReader的用法的一个难题,EndElement

如下,CodeGuru或者CodeProject上找到的例子都没有处理 像这样的情形 :
<a name="test" />  就是没有 </a>标签的情形,这时候处理a元素是不会碰到 EndElement的。
这里做了一个处理,即判断其深度。
None.gif XmlTextReader reader  =   null ;
None.gif            
try
ExpandedBlockStart.gifContractedBlock.gif            
dot.gif {
InBlock.gif                reader 
= new XmlTextReader(streamReader);
InBlock.gif                startDocument(); 
//开始分析
InBlock.gif

InBlock.gif                
int dept = -1;
InBlock.gif                
while (reader.Read())
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    
switch (reader.NodeType)
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
dot.gif{                        
InBlock.gif                        
case XmlNodeType.Element:
InBlock.gif                            Hashtable attributes 
= new Hashtable();
InBlock.gif                            
string strURI = reader.NamespaceURI;
InBlock.gif                            
string strName = reader.Name;
InBlock.gif                            
if (reader.HasAttributes)
ExpandedSubBlockStart.gifContractedSubBlock.gif                            
dot.gif{
InBlock.gif                                
for (int i = 0; i < reader.AttributeCount; i++)
ExpandedSubBlockStart.gifContractedSubBlock.gif                                
dot.gif{
InBlock.gif                                    reader.MoveToAttribute(i);
InBlock.gif                                    attributes.Add(reader.Name, reader.Value);
ExpandedSubBlockEnd.gif                                }

ExpandedSubBlockEnd.gif                            }

InBlock.gif                            startElement(strURI, strName, strName, attributes);
InBlock.gif                            
if(dept != -1 && reader.Depth <= dept)                        //处理 "/>"的标签
InBlock.gif
                                endElement(string.Empty, reader.Name, string.Empty);
InBlock.gif                            dept 
= reader.Depth;
InBlock.gif                            
break;
InBlock.gif                        
case XmlNodeType.EndElement:
InBlock.gif                            endElement(
string.Empty, reader.Name, string.Empty);
InBlock.gif                            
break;
InBlock.gif                        
case XmlNodeType.Text:
InBlock.gif                            characters(reader.Value);
InBlock.gif                            
break;
InBlock.gif                        
default:
InBlock.gif                            
break;
ExpandedSubBlockEnd.gif                    }

ExpandedSubBlockEnd.gif                }

InBlock.gif                
return (Namespace) objectStack.Pop();
ExpandedBlockEnd.gif            }

None.gif            
catch  (XmlException e)
ExpandedBlockStart.gifContractedBlock.gif            
dot.gif {
InBlock.gif                Console.WriteLine(
"error occured: " + e.Message);
ExpandedBlockEnd.gif            }

None.gif            
finally
ExpandedBlockStart.gifContractedBlock.gif            
dot.gif {
InBlock.gif                
if(reader != null) reader.Close();
ExpandedBlockEnd.gif            }

转载于:https://www.cnblogs.com/xiaotaoliang/archive/2006/03/15/350867.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值