Altova Mailing List Archives>Archive Index >microsoft.public.xml Archive Home >Recent entries >Thread Prev - Avoid NodeType Comment >Thread Next - Re: Avoid NodeType Comment Re: Avoid NodeType CommentTo: NULL Date: 3/3/2006 4:03:00 PM
Rasmus wrote:
> But the xsd allows this xml to pass:
>
> <book>
> <!-- a comment -->
> <author id="1" />
> </book>
>
> In my code (c#) i do:
>
> string id = bookNode.FirstChild.Attributes["id"];
Then don't do it that way, there are much safer and much more elegant
ways, with .NET you can simply do e.g.
XmlElement authorElement = bookNode["author"] as XmlElement;
if (authorElement != null) {
id = authorElement.GetAttribute("id");
}
Don't rely on child node positions to access elements, white space,
comments, processing instruction can all change the position of an
element. Simply use other access methods like above or like XPath
(SelectSingleNode("author")) where you explictly access elements if you
are looking for elements.
--
Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/
| ||||||
| Company | Legal | Press | Partners | Careers | Sitemap | Contact Us | Altova Blog | Mobile | Full Site | |||
|
