Finally tired of the "System.Xml.XmlValidatingReader is obsolete" message, I undertook to replace it with the recommended XmlReader. The obsolescence documentation from MS wasn't much help, but I did manage to create code that compiled, as follows:
string nameSpace = GetSchemaNamespace(_XmlDocumentStream);
XmlTextReader schemaReader = new XmlTextReader(_SchemaStream);
XmlSchemaSet xsc = new XmlSchemaSet();
XmlNameTable nt = new NameTable();
XmlNamespaceManager nsmgr = new XmlNamespaceManager(nt);
XmlParserContext parserContext = new XmlParserContext(nt, nsmgr, null, XmlSpace.None);
XmlReaderSettings settings = new XmlReaderSettings();
settings.ValidationType = ValidationType.Schema;
settings.ValidationFlags |= XmlSchemaValidationFlags.ReportValidationWarnings;
settings.ValidationEventHandler += new ValidationEventHandler(ValidationEventHandler);
XmlReader reader = XmlReader.Create(_XmlDocumentStream, settings, parserContext);
And then I found these two things I don't understand:
1. First, the Create failed because this attribute in the xsd wasn't defined.
There was no complaint over this from the obsolete class. From the error message, I assume that this line previously covered that definition:
2. I removed the references to the above attribute so I could get a reader created and ran our set of unit tests for this process. The next surprise was that the reader doesn't handle validation errors the same as before. (a) Undefined attributes used to be Warnings. Now they are Errors. So what is now considered a warning? (b) It used to continue on from errors, allowing us to compile a list of errors and warnings for the XML being validated. Now, it bails out after the first error and I never get more than one invocation of the ValidationEventHandler. The test case that used to yield 43 errors and 21 warnings now produces a single error (and the test therefore fails). Is there a way to get the former behavior back?
I don't see a way to use that suggestion with the parts that I have. The errors of interest are not going to be found by the default serializer; they are schema conformance errors, relating to a specific xsd, and not plain XML errors.
I have some data in Excel which represents structured lists to be imported into a SQL database in an XML format. I am not so familiar with the nuances of XML but I am pretty good with VBA. I need to generate an XML file that looks like this.
<?xmlversion="1.0"?><!DOCTYPExml><!--To use the dtd in an external file: <!DOCTYPE xml SYSTEM "filename.dtd">-->
-<transactionvaultname="Type in the name of the vault here"date="123459"type="import_lists"><!-- Just a list without links -->
-<listname="Materials"><itemitem_name="Ceramic"/><itemitem_name="Ferrous Metal"/><itemitem_name="Nonferrous Metal"/><itemitem_name="Superalloy"/></list><!-- A set of linked lists -->
-<listname="Ceramics"><itemitem_name="Boride"/><itemitem_name="Carbide"/><itemitem_name="Nitride"/><linklink_value="Ceramic"list_variable="Material Type"source_variable="My Material"/></list>
-<listname="Ferrous Metals"><itemitem_name="Stainless Steel"/><itemitem_name="Carbon Steel"/><itemitem_name="Tool Steel"/><linklink_value="Ferrous Metal"list_variable="Material Type"source_variable="My Material"/></list>
-<listname="Nonferrous Metals"><itemitem_name="Aluminium Alloy"/><itemitem_name="Copper Alloy"/><itemitem_name="Tin Alloy"/><linklink_value="Nonferrous Metal"list_variable="Material Type"source_variable="My Material"/></list>
-<listname="Superalloys"><itemitem_name="Cobalt Base"/><itemitem_name="Iron Base"/><itemitem_name="Nickel base"/><linklink_value="Superalloy"list_variable="Material Type"source_variable="My Material"/></list></transaction></transactions></xml>
I have the data in Excel and I would like to use VBA code similar to the below, but I am having a devil of a time getting it to output as shown above. When I change the tags to what I need in the sample output the result is just a mess. I am sure its my limited exposure to XML that has me stumbling. If someone could show me a sample set of the VBA that would output some of the above it would get me rolling again.
<?xmlversion="1.0"encoding="UTF-8"?><!-- Edited by XMLSpy --><catalog><example>
:20:FT13261793408907 N23B:CRED SA32A:130918USD111670,00
output must not be in a same line its must be as shown in the desired o/p
Hi all, I have already raised my question in different area of this forum. Please have a look at my question from the below link for more information. Could you please help me to generate xml structure with mutiple namespces. TIA.
I'd like to serialize an Objekt with XmlSerializer. The Objekt is a kind of treeview with lots of public properties like this:
public object Instance;
Currently I have different kind of classes stored in the Instance objects, all inheriting from the same base class. If I want to serialize these, I need to tell the XmlSerializer in an XmlAttributeOverrides object about the different Instance object classes. I collect this information dynamically before starting the XmlSerializer.
Setting a breakpoint just before that I can check the Overrides object which looks like this:
Now here's my problem: I get a InvalidOperationException: "The type CTrModules.MDataSaveXML was not expected."
Looking through the Overrides above, CTrModules.MDataSaveXML was handed over to the serializer along with all other classes. All other classes can be serialized, but not this one. All classes have the Serializable attribute and are inheriting the same base class.
I'm at a loss here. Why is CTrModules.MDataSaveXML not expected?
I'm using C# in Visual Studio Espress 2013 for Windows Desktop, the target framework is .NET Framework 4.
Did you add the [XmlInclude(typeof(CTrModules.MDataSaveXML))]
attribute to the base class
I don't really want to go with that option, because then I have to remember to add such an include for any class I derive from the base class.
But I tried it just for test purposes: I get the same problem.
The option I chose to use is this one:
Bernhard Hiller wrote:
or pass an array of types to the constructor of the serializer?
I wrote a method to gather all types in an array and I pass this array to the serializer.
I set a breakpoint at the call of the serializer and inspected this array:
Guys, give me your thoughts on how you validate your xml at runtime. I recently fell into a drawn-out debug session where I couldn't see the problem with my xml file. I opened it in c#, read it, and checked for various attributes and what have you, adopting plan B that if I don't find an attribute I'll assume a default value.
The problem was I had an attribute called Recursion but I inadvertently spelt it Recursive, not the same thing!
I know I could validate the xml with an xsd file which sounds great in principal but is that the preferred way to validate your xml or do you do it the "safer" (word used very loosely) way by keeping all the literal names in your code and then issue warnings if things don't pass muster?
The xsd file itself could for any number of reasons be subject to an incorrect edit. I'd like to know which way you do things to ensure that when it comes to the usage, your xml is as clean to use as you can make it. Ideas?
If there is one thing more dangerous than getting between a bear and her cubs it's getting between my wife and her chocolate.