Click here to Skip to main content
15,892,059 members
Home / Discussions / C#
   

C#

 
AnswerRe: HTML Table tag text Scraping Pin
PIEBALDconsult19-Jul-10 4:21
mvePIEBALDconsult19-Jul-10 4:21 
QuestionReading user/group of your local machine Pin
dabuskol19-Jul-10 0:39
dabuskol19-Jul-10 0:39 
QuestionHi Pin
KORCARI18-Jul-10 23:27
KORCARI18-Jul-10 23:27 
AnswerRe: Hi Pin
Mycroft Holmes19-Jul-10 2:57
professionalMycroft Holmes19-Jul-10 2:57 
GeneralRe: Hi Pin
KORCARI19-Jul-10 3:19
KORCARI19-Jul-10 3:19 
QuestionSending XML over a Socket connection Pin
Priya Prk18-Jul-10 22:53
Priya Prk18-Jul-10 22:53 
AnswerRe: Sending XML over a Socket connection Pin
Peace ON18-Jul-10 23:08
Peace ON18-Jul-10 23:08 
QuestionRead both attributes and subtree of an XML element using XmlTextReader Pin
PPKeno18-Jul-10 22:05
PPKeno18-Jul-10 22:05 
I have an xml file that looks like this:
<root>
  <element1 />
  <element2 attr1="text" attr2="text">
    <subelement21>
      <subsubelement attr1="text" attr2="text" />
      <subsubelement attr1="text" attr2="text" />
      ...
      <subsubelement attr1="text" attr2="text" />
    </subelement21>
  </element2>
</root>


I am trying to read this file using a XmlReader. This is what I've tried but failed:

XmlTextReader reader = new XmlTextReader("file.xml");
while(reader.Read()) {
  if (reader.NodeType == XmlNodeType.Element) {
    switch (reader.Name) {
      case "element1":
        break;
      case "element2":
        reader.MoveToAttribute("attr1");
        String att1 = reader.Value;
        reader.MoveToAttribute("attr2");
        String att2 = reader.Value;
        
        XmlReader subreader = reader.ReadSubTree();
        while(subreader.Read()) {
          if(subreader.NodeType == XmlNodeType.Element) {
            switch (reader.Name) {
              case "subsubelement":
                subreader.MoveToAttribute("attr1");
                String a1 = reader.Value;
                subreader.MoveToAttribute("attr2");
                String a2 = reader.Value;
                break;
          }
        }
        break;
    }
  }
}


Here i get an exception saying that ReadSubTree() can't be called since reader is no longer at an element node, which makes sense. If I change the order and try to first read the sub tree and then the attributes of element2, i get the problem that reader is at the end element after reading the sub tree. This also makes sense, but i can't find a way to solve this.

Any ideas? I appreciate any suggestions.

Thanks
AnswerRe: Read both attributes and subtree of an XML element using XmlTextReader Pin
Peace ON18-Jul-10 22:34
Peace ON18-Jul-10 22:34 
QuestionReading .dbc file in C# Pin
Niungareamit18-Jul-10 20:14
Niungareamit18-Jul-10 20:14 
AnswerRe: Reading .dbc file in C# Pin
V.18-Jul-10 20:32
professionalV.18-Jul-10 20:32 
AnswerRe: Reading .dbc file in C# Pin
Richard MacCutchan18-Jul-10 22:33
mveRichard MacCutchan18-Jul-10 22:33 
QuestionReading Microsoft Project file problem Pin
DJ24518-Jul-10 17:42
DJ24518-Jul-10 17:42 
AnswerRe: Reading Microsoft Project file problem Pin
Peace ON18-Jul-10 22:49
Peace ON18-Jul-10 22:49 
QuestionC# version of C++ code Pin
gordon305618-Jul-10 16:06
gordon305618-Jul-10 16:06 
AnswerRe: C# version of C++ code [modified] Pin
JF201518-Jul-10 18:08
JF201518-Jul-10 18:08 
AnswerRe: C# version of C++ code Pin
Dave Doknjas19-Jul-10 10:52
Dave Doknjas19-Jul-10 10:52 
QuestionHow to draw and smooth Polyline. Pin
tranvantinhpt18-Jul-10 16:05
tranvantinhpt18-Jul-10 16:05 
AnswerRe: How to draw and smooth Polyline. Pin
Abhinav S18-Jul-10 18:24
Abhinav S18-Jul-10 18:24 
GeneralRe: How to draw and smooth Polyline. Pin
tranvantinhpt18-Jul-10 18:42
tranvantinhpt18-Jul-10 18:42 
QuestionCopying table Pin
humayunlalzad18-Jul-10 13:51
humayunlalzad18-Jul-10 13:51 
AnswerRe: Copying table Pin
Mycroft Holmes18-Jul-10 14:29
professionalMycroft Holmes18-Jul-10 14:29 
GeneralRe: Copying table Pin
harold aptroot18-Jul-10 14:38
harold aptroot18-Jul-10 14:38 
GeneralRe: Copying table Pin
humayunlalzad18-Jul-10 17:53
humayunlalzad18-Jul-10 17:53 
AnswerRe: Copying table Pin
PIEBALDconsult18-Jul-10 15:56
mvePIEBALDconsult18-Jul-10 15:56 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.