Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / XML / March 2007

Tip: Looking for answers? Try searching our database.

Node Type both Element and Text

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Kevin - 19 Mar 2007 01:09 GMT
I have an XML node that has both attributes and text.  I am having difficulty
getting the xmlreader to deal with this.

Sample XML Node:
<HomePhoneNumber Attribute1="Y" Attribute2="Y" Attribute3="Y">(425)
555-1212</HomePhoneNumber>

I'd like to iterate on this and output the node's path, name, each attribute
name and value pair, as well as the phone number itself.
Martin Honnen - 19 Mar 2007 14:12 GMT
> I have an XML node that has both attributes and text.  I am having difficulty
> getting the xmlreader to deal with this.
[quoted text clipped - 5 lines]
> I'd like to iterate on this and output the node's path, name, each attribute
> name and value pair, as well as the phone number itself.

Do it like this:

      using (XmlReader xmlReader = XmlReader.Create(@"file.xml")) {
        while (xmlReader.Read()) {
          if (xmlReader.NodeType == XmlNodeType.Element) {
            if (xmlReader.Name == "HomePhoneNumber") {
              Console.WriteLine("Element \"{0}\":", xmlReader.Name);
              while (xmlReader.MoveToNextAttribute()) {
                Console.WriteLine("Attribute {0}={1}", xmlReader.Name,
xmlReader.Value);
              }
              Console.WriteLine("Element content: \"{0}\"",
xmlReader.ReadString());
              Console.WriteLine();
            }
          }
        }
      }

Signature

    Martin Honnen --- MVP XML
    http://JavaScript.FAQTs.com/

Kevin - 23 Mar 2007 05:51 GMT
Great thanks - I should explain my problem more - I am trying to write a
function that traverses an XML document and outputs the full  path
(/Node1/Node2/Node3), and a list of attribute/value pairs, as well as the
content of the node.  

The below solution is able to output the latter 2, but path seems difficult
to get from XmlReader and I find myself writing a recursive function to get
this.  

For example, an an input file:
<root>
<node1 attributeX="Y">XXX</node1>
<node2>
<node3 attributeY="N">YYY</node3>
</node2>
</root>

would output the following:

Path,Value
-------------------
/root/node1/AttributeX,Y
/root/node1,XXX
/root/node2/node3/attributeY,N
/root/node2/node3,YYY

> > I have an XML node that has both attributes and text.  I am having difficulty
> > getting the xmlreader to deal with this.
[quoted text clipped - 24 lines]
>          }
>        }

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.