开发者

Does DataSet.ReadXML() Validates the XML against the DTD

开发者 https://www.devze.com 2023-03-22 17:15 出处:网络
I have an xml file and I am loading 开发者_JAVA技巧it in DataTable using DataSet.ReadXML. This XML has an internal DTD Defined. I thought DataTable.ReadXML Validates an XML before loading it in memory

I have an xml file and I am loading 开发者_JAVA技巧it in DataTable using DataSet.ReadXML. This XML has an internal DTD Defined. I thought DataTable.ReadXML Validates an XML before loading it in memory. Is it the case ?

Do I need to set some property within my dataset to make it validate Xml against the DTD Defined or Do I need to validate it using some othe XML class

<?xml version="1.0" standalone="yes"?>

<!DOCTYPE Resources [
<!ELEMENT Resources (Resource)+>
<!ELEMENT Resource (ResourceName,ResourceEmail)>
<!ELEMENT ResourceName (#PCDATA)>
<!ELEMENT ResourceEmail (#PCDATA)>

]>

<Resources>
<Resource>
<ResourceName>test</ResourceName>
<ResourceEmail>dfjfhg@fkjg.com</ResourceEmail>
</Resource>
</Resources>


You can use this code to validate your XML against a DTD

XmlReaderSettings settings = new XmlReaderSettings();
               settings.DtdProcessing = DtdProcessing.Parse;
               settings.ValidationType = ValidationType.DTD;

               string data = null;
               XmlReader validatingReader = XmlReader.Create(File.Open("C:\\check\\TEMP_DTD.XML", FileMode.Open), settings, data);

                DataSet ds = new DataSet();
                ds.ReadXml(validatingReader); 


You need to use the XmlReadMode enumeration with the ReadXML method:

DataSet.ReadXml(Stream|String|TextReader|XmlReader, XmlReadMode.ReadSchema)

e.g.

string xml = // your xml here; can also use Stream, TextReader or XmlReader

DataSet.ReadXml(xml, XmlReadMode.ReadSchema);

DataSet.ReadXml Method

XmlReadMode Enumeration

EDITED TO ADD

If you're looking to validate the XML, it appears you need to use XmlReader and create a "validating" reader. Take a look at this post:

DataSet does not validate XML Schema (XSD)


        var errors = new StringBuilder();
        var isValid = true;
        var settings = new XmlReaderSettings();
        settings.ValidationEventHandler += (o, e) => { errors.AppendLine(e.Message); isValid = false; };
        settings.ValidationType = ValidationType.DTD;
        settings.DtdProcessing = DtdProcessing.Parse;


        var content = @"<?xml version=""1.0"" standalone=""yes""?>
                            <!DOCTYPE Resources [
                            <!ELEMENT Resources (Resource)+>
                            <!ELEMENT Resource (ResourceName,ResourceEmail)>
                            <!ELEMENT ResourceName (#PCDATA)>
                            <!ELEMENT ResourceEmail (#PCDATA)>                              
                            ]>                          
                        <Resources>
                            <Resource>
                                <ResourceName>test</ResourceName>
                                <ResourceEmail>dfjfhg@fkjg.com</ResourceEmail>

                                <YourPlaceIsNotHere>asdasd</YourPlaceIsNotHere>


                            </Resource>
                        </Resources>";
        using (var validator = XmlReader.Create(new StringReader(content), settings))
        {
            while (validator.Read())
            {
                if (!isValid)
                {
                    validator.Close();
                    break;
                }
            }
        }

A solution is to validate the xml before loading into a DataSet, using this code.

0

精彩评论

暂无评论...
验证码 换一张
取 消