.net - XmlReader get element default from schema -


This is probably a naive question about XmlReader , but I have not responded to MSDN Docs.

Suppose I have XSD SchemaTest.xsd

   & Lt; Xs: element name = "height" type = "x: decimal" default = "11" minOccurs = "0" /> & Lt; / XS: sequence & gt; & Lt; / XS: complexType & gt; & Lt; / XS: element & gt; & Lt; / XS: Schema & gt;  

Well-formed XML document SchemaTest.xml is consistent with this schema

  & Lt;? Xml version = "1.0" encoding = "UTF-8"? & Gt; & Lt; Page Settings xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance" xsi: Any namespace location = "schemast.exd" & gt; & Lt; Width / & gt; & Lt; Height & gt; 11 & lt; / Height & gt; & Lt; / PageSettings & gt;  

And that I read this document as XmlReader as follows.

  Fixed zero main (string [] arg) {decimal width; Decimal height; XmlReaderSettings Settings = New XmlReaderSettings (); Settings.IgnoreWhitespace = true; Settings. Chimes Add (empty, "C: \\ projects \\ schemaest \\ schema test \\ schematest .xsd"); (XmlReader Reader = XmlReader.Create ("C: \\ Projects \\ schematest \\ schematest \\ schematist.xmm", settings)) {reader.ReadStartElement (); If (reader.Name == "width") {width = reader.ReadElementContentAsDecimal ("width", ""); // If unsuccessful, width = default from schema} if (reader.Name == "height") {height = reader.ReadElementContentAsDecimal ("height", ""); // If unsuccessful, height = default from schema} reader. Readendelament (); }}  

Currently I get a system.form exposure , which indicates that the content is not in the correct format on element It seems that the reader element is trying to read the content and that is not the default for the default value specified in the schema. What is the right way to handle it?

In addition, it is my understanding that for the elements, the schema provides only a default value if an element appears with empty content but does not provide the default value element is missing what its The meaning is that there is no way to get a default value for alternative elements?

To work with empty elements, type Set Schema and you should get the default value in the desired format.

Regarding missing elements, they are considered, well, is missing; -) and thus does not have a default value. Theoretically, you can parse the schema (using the example schema object model) to get the default value. But it will be against the device.

Do you have attributes, such as & lt; Have considered using page settings height = "55" /> ? In that scenario, you should get the default value for missing attributes.


Comments

Popular posts from this blog

python - Overriding the save method in Django ModelForm -

html - CSS autoheight, but fit content to height of div -

qt - How to prevent QAudioInput from automatically boosting the master volume to 100%? -