开发者

text between any tag

开发者 https://www.devze.com 2023-02-28 00:55 出处:网络
<name>Tag 6</name> using XmlTextReader how can i re开发者_如何学编程ad the value of thetext between the name tag
<name>Tag 6</name> 

using XmlTextReader how can i re开发者_如何学编程ad the value of the text between the name tag .ie how can i get Tag 6


With ReadElementString function:

var text = string.Empty;
using (var reader = new XmlTextReader(filename))
{
    reader.MoveToContent();
    text = reader.ReadElementString(); // read content text from current node
    reader.Close();
}


From the docs I'm guessing you need to do something like

ReadToDescendant http://msdn.microsoft.com/en-us/library/ayf5ffy5.aspx

to find the name node.

then

ReadInnerXml to get the content.

http://msdn.microsoft.com/en-us/library/system.xml.xmlreader.readinnerxml.aspx

not used it though


while (reader.Read()){
   if(reader.NodeType == XmlNodeType.Element && reader.Name == "name"){                               
       this.tagXml.Append("<").Append(reader.Name).Append(">");
       currentTag = reader.Name.Trim();
       //first loop go through this
   }                   
   if(reader.NodeType== XmlNodeType.Text){
      //second loop go through this
      if (currentTag == "name"){
        this.tagXml.Append(reader.Value);
      }
   }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消