HighTechTalks DotNet Forums  

check XmlDeclaration

Dotnet XML microsoft.public.dotnet.xml


Discuss check XmlDeclaration in the Dotnet XML forum.



Reply
 
Thread Tools Search this Thread Display Modes
  #1  
Old   
xke
 
Posts: n/a

Default check XmlDeclaration - 10-23-2007 , 07:37 PM






How can I check that a document has XMLDeclaration ?

i.e. it has <?xml version="1.0" encoding="utf-8"?> in the beginning

thanks,
xke


Reply With Quote
  #2  
Old   
Joe Fawcett
 
Posts: n/a

Default Re: check XmlDeclaration - 10-24-2007 , 06:00 AM






"xke" <xkeops (AT) gmail (DOT) com> wrote

Quote:
How can I check that a document has XMLDeclaration ?

i.e. it has <?xml version="1.0" encoding="utf-8"?> in the beginning

thanks,
xke

You can't using standard XML/DOM/XSLT methods. The declaration is only
really there to help the (de)serialisation and is not part of the infoset.

--

Joe Fawcett (MVP - XML)

http://joe.fawcett.name





Reply With Quote
  #3  
Old   
Martin Honnen
 
Posts: n/a

Default Re: check XmlDeclaration - 10-24-2007 , 08:53 AM



xke wrote:
Quote:
How can I check that a document has XMLDeclaration ?

i.e. it has <?xml version="1.0" encoding="utf-8"?> in the beginning
If you load the document with XmlReader then you can check the
XmlNodeType after the first Read call e.g. pseudo code
public static bool HasXmlDeclaration (string fileName)
{
using (XmlReader reader = XmlReader.Create(fileName))
{
reader.Read();
return reader.NodeType == XmlNodeType.XmlDeclaration;
}
}
Console.WriteLine(HasXmlDeclaration("file.xml"))

If you use System.Xml.XmlDocument then you can check whether
xmlDocumentInstance.FirstChild is an XmlDeclaration node.

--

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


Reply With Quote
Reply




Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.4
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.