0
<?xml version="1.0" encoding="utf-8"?>
<xml>
  <a>str1234</a>xxxx
</xml>

I got this xml file, as you can see, there's "xxxx" after the "a" close tag.

I tried the xmldocument.load() method but it wouldn't throw any exceptions.

I tried to generate a xsd file from this xml, then validate this xml with the generated xsd.

However, it also wouldn't throw any errors.

ElysiuM
  • 3
  • 1
  • "Probably" because this is valid xml ... – mybirthname Apr 01 '17 at 08:48
  • But when my code loading this kind of .config file,it got stuck. – WeiChinHsing Apr 01 '17 at 08:56
  • are you going to serialize and get the output ? or what is your need? please specify clearly – Chandan Kumar Apr 01 '17 at 09:19
  • ohhh k. this is a valid xml file. why it will show error? – Chandan Kumar Apr 01 '17 at 09:25
  • Just to echo other comments, this is perfectly valid XML. You need to provide a [mcve] that demonstrates the problem you're having with it. What is *'the code loading this kind of .config file'* and what do you mean by *'it got stuck'*? – Charles Mager Apr 01 '17 at 10:39
  • Commenters offering help should take care to [**use *valid* and *well-formed* properly**](http://stackoverflow.com/questions/134494/is-there-a-difference-between-valid-xml-and-well-formed-xml). They have precise meanings that beginners should understand. See that link and [**my answer below**](http://stackoverflow.com/a/43157821/290085) for details. – kjhughes Apr 01 '17 at 14:03

1 Answers1

1

It is important to understand the difference between valid and well-formed XML.

Commenters have sloppily said that your XML is valid. They actually should not make such a statement without a schema against which to assess validity. They should be saying that your XML is well-formed.

You seem to be concerned that xxxx text as a sibling to an a element is not well formed, but it is perfectly well-formed XML. It might also be valid, if the parent element, xml is defined by a schema to allow mixed-content.

I tried to generate a xsd file from this xml, then validate this xml with the generated xsd.

Well, if you used a tool to generate an XSD from an XML document instance, and the XSD said the XML was valid, then the tool is working as designed.

But when my code loading this kind of .config file,it got stuck.

Just like being well-formed doesn't guarantee validity, it also doesn't guarantee that it meets the needs of any given consuming XML application. A configuration file has rules, perhaps expressed in an XSD, that the XML must follow. These rules are in addition to being well-formed (the rules that a parser requires in order to parse XML).

See also How to validate xml code file though .NET? + How would I do it if I use XML serialization?

Community
  • 1
  • 1
kjhughes
  • 106,133
  • 27
  • 181
  • 240
  • Great answer,dude.It solved my problem.All I need is to find out complextype is mixed or not.If it's mixed, it's not the right xml I want. – WeiChinHsing Apr 02 '17 at 03:39
  • You're welcome. Please [**accept**](http://meta.stackoverflow.com/q/5234/234215) this answer if it's helped. Thanks. – kjhughes Apr 02 '17 at 04:07