• 0

[C# .Net] Reading XML Attributes


Question

Hey,

I tried searching for information but couldn?t find any useful information.

Does anyone know how to read Attribute strings using XMLtextreader?

 <?xml version="1.0" ?> 
- <Notes>
  <Note ID="903410613" text="test" x="20" y="33" pin="false" opacity="255" /> 
  <Note ID="903415410" text="test" x="60" y="443" pin="false" opacity="255" /> 
  <Note ID="903418395" text="test" x="43" y="450" pin="false" opacity="255" /> 
  </Notes>

Thanks for any information.

Link to comment
Share on other sites

6 answers to this question

Recommended Posts

  • 0

Example from MSDN:

//Load the reader with the XML file.
       reader = new XmlTextReader("attrs.xml");
  
       //Read the genre attribute.
       reader.MoveToContent();
       reader.MoveToFirstAttribute();
       string genre=reader.Value;
       Console.WriteLine("The genre value: " + genre);

If that doesn't do what you want, check out the XmlAttribute and XmlAttributeCollection classes

edit: I got exceptions with your file until I changed the top line:

<?xml version="1.0" encoding="utf-8" ?>
<Notes>
    <Note ID="903410613" text="test" x="20" y="33" pin="false" opacity="255" />
    <Note ID="903415410" text="test" x="60" y="443" pin="false" opacity="255" />
    <Note ID="903418395" text="test" x="43" y="450" pin="false" opacity="255" />
</Notes>

Edited by JJ6829
Link to comment
Share on other sites

  • 0

Hey,

Thanks for your reply. That code doesn't want to work for some reason :/ . I will have a look into XmlAttribute and XmlAttributeCollection classes tho.

Thanks again :)

Link to comment
Share on other sites

  • 0

Well I have a bit of it working,

           XmlTextReader reader = new XmlTextReader("notes.xml");
            reader.ReadStartElement();
            reader.MoveToContent();
            reader.MoveToAttribute("text");
            string ID = reader.Value;

It only scans the first line though. I need it to scan every line for the "text" value.

Any idea? :unsure:

Link to comment
Share on other sites

  • 0

Just use a loop to, err, loop through each line. And use XmlDocument/XmlNode/XmlAttribute - I find it much easier. I haven't tried this code, but it should work (I hope):

XmlTextReader reader = new XmlTextReader("notes.xml");
XmlDocument doc = new XmlDocument();
doc.Load(reader);

XmlNode baseNode = doc.DocumentElement;

foreach (XmlNode node in baseNode.SelectNodes("/Notes/Note")
{
  // Do something here.
  Console.WriteLine(node.Attributes["text"].Value + "\n");
}

Link to comment
Share on other sites

  • 0

well MoveToAttribute returns a bool.

while ( true == reader.MoveToAttribute("text") )

{

//blahblah

}

Get into the habit of putting constants on the left of the comparison operator, that way you will never have logic errors with confusing = with ==. There will be a compiler error if you have = instead of ==. If the constant is on the right side and you do a =, it will compile and you'll never know.

Link to comment
Share on other sites

  • 0
Just use a loop to, err, loop through each line. And use XmlDocument/XmlNode/XmlAttribute - I find it much easier. I haven't tried this code, but it should work (I hope):

XmlTextReader reader = new XmlTextReader("notes.xml");
XmlDocument doc = new XmlDocument();
doc.Load(reader);

XmlNode baseNode = doc.DocumentElement;

foreach (XmlNode node in baseNode.SelectNodes("/Notes/Note")
{
  // Do something here.
  Console.WriteLine(node.Attributes["text"].Value + "\n");
}

Hey,

Thanks, that worked :)

Link to comment
Share on other sites

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.