简体   繁体   中英

validate xml string content including encoding using C#

I need to validate a string that contains XML Data, there is no schema validation required. All I need to do is make sure that the XML is well formed and properly encoded. For example, I want my code to identify this snippet of XML as invalid:

<?xml version="1.0" encoding="utf-8"?>
<parentNode> Positions1 ’</parentNode>

Using the LoadXML method in XMLDocument does not work, there are no errors thrown when I load the snippet above.

I am aware of how to do this if the content were in an XML file, the following snippet of code shows that:

XmlReaderSettings readerSettings = new XmlReaderSettings();
readerSettings.ConformanceLevel = ConformanceLevel.Document;
readerSettings.CheckCharacters = true;
readerSettings.ValidationType = ValidationType.None;

xmlReader = XmlReader.Create(xmlFileName, readerSettings);
XmlDocument xdoc = new XmlDocument();
xdoc.Load(xmlReader);

So short of creating a temporary file to write out my xml string content and then creating an XmlReader instance to read it, is there any alternative? Appreciate much if someone could guide me in the right direction with this problem.

You have not fully understand what encoding means. If you have a.Net string in memory, it's no more "raw data" and has no encoding for that reason. And so LoadXML ingores for a good reason. So what you want to do makes not much sense at all. But if you really want to do it:

You can convert your string into a in memory stream, so you don't have to write a temporary file. Then you can use that stream instead of the xmlFileName in your call to XmlReader.Create.

Achim,

Thanks for your detailed replies, I was able to finally come up with a solution that fits my needs. It involves grabbing the bytes out of the 'unicode' string and then transforming the bytes to utf8 encoding.

        try
        {
            byte[] xmlContentInBytes = new System.Text.UnicodeEncoding().GetBytes(xmlContent);

            System.Text.UTF8Encoding utf8 = new System.Text.UTF8Encoding(false, true);
            utf8.GetChars(xmlContentInBytes);
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.Message);
            return false;
        }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM