1

I'm relatively new to using C#'s XML classes. I can't even get an XML reader to recognize that the string I'm passing to it is XML. Here is my unit test which I'm using to test basic Xml reading

[TestFixture()]
public class LegacyWallTests
{
    [Test()]
    public void ReadLegacyWallFile()
    {
        var legacyWallText = legacyfiles.legacywall1;
        {
            string xmlString = legacyfiles.legacywall1;
            using (XmlReader reader = XmlReader.Create(new StringReader(xmlString)))
            {
                reader.HasAttributes.Should().BeTrue();
            }
        }
    }
}

And here is the XML I'm trying to read

<Wall>
  <Actual>
    <Specifications>
      <Insertion> 375.6858 916.8871 0.0000 </Insertion>
      <Angle> 3.14159 </Angle>
      <WallDesc> E4-1, H: 8' 1 1/8, Sh: Yes, S: 2~4~2~9-0-0~SPF~~, Spc: Single @ 16 in OC, BP: 2~4~2~12-0-0~SYP~~, CP: 2~4~2~12-0-0~SYP~~, TP: 2~4~2~12-0-0~SYP~~,\P LI: Single @ 38.75000000, CB: No, VB: No, NCT: 2~4~2~9-0-0~SPF~~, CT: 2~4~2~9-0-0~SPF~~, Pac: 2~4~2~9-0-0~SPF~~, Mir: Yes </WallDesc>
      <WallNum> 1 </WallNum>
      <VaporBarrier></VaporBarrier>
    </Specifications>
  </Actual>
</Wall>

legacyfiles.legacywall1 is the name of the xml file I added to my project's resources. I know that the xml file is being read because outputting that string to the console gives me the xml from the file. However, when I create the XmlReader and test that there are attributes it says there aren't any. I don't know what I'm doing wrong.

Alexei Levenkov
  • 98,904
  • 14
  • 127
  • 179
Nick Gilbert
  • 4,159
  • 8
  • 43
  • 90
  • 2
    To answer your exact question "what I'm doing wrong" - not checking documentation and not providing good title to your post. – Alexei Levenkov Sep 17 '15 at 15:29
  • Side note: XML in the post has *no attributes* so even if you actually read through it there will never be case when `HasAttributes` is true - you may want to update post to show XML with attributes. – Alexei Levenkov Sep 17 '15 at 15:30
  • The application I'm writing has to read XML as is so I need a method that can read this XML as is... – Nick Gilbert Sep 17 '15 at 15:38

1 Answers1

5

XmlReader.HasAttribute returns true if the current node has attributes. As you don't advance into the document, the reader starts at the root element, <Wall>, which doesn't have attributes. Nor do any other of your elements.

An attribute is bar in <foo bar="baz" />.

You also generally don't want to mess with XML using readers. Obtain or generate an XSD (also very useful for input validation), generate a class from that XSD and deserialize the incoming XML to an instance of that class. Then you can just access wall.Actual.Specifications[0].WallDesc.

CodeCaster
  • 147,647
  • 23
  • 218
  • 272
  • So if I did want to continue using Reader how would I get the string in between the tags? – Nick Gilbert Sep 17 '15 at 15:26
  • 1
    @Nick see [How can I read specific elements from XML string using XMLREADER in C#](http://stackoverflow.com/questions/8888806/), [Parsing through XML elements in XmlReader](http://stackoverflow.com/questions/243022/). The latter, XPath, is preferred. – CodeCaster Sep 17 '15 at 15:29
  • That XML is set up different then mine. It has attributes. In my application I have to read the XML as is, I can't change it – Nick Gilbert Sep 17 '15 at 15:35
  • @Nick of course it's different, it's someone else's. You can apply the concepts explained there though. Check the web for an XPath tutorial. Something like `/Wall/Actual/Specifications/Insertion`. – CodeCaster Sep 17 '15 at 15:39