Jump to content

Create Xml From Binary Using Schema


seacdsl

Recommended Posts

I have some binary data that I wish to convert to xml, using the schema as below, or something like it, but I cant see exactly how to populate the XmlDocument with my data.

<?xml version="1.0" encoding="utf-8"?><xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">    <xs:element name="logResults">	<xs:complexType>	  <xs:attribute name="value" type="xs:decimal" />	  <xs:attribute name="speed" type="xs:unsignedShort" />	  <xs:attribute name="time" type="xs:int" />	</xs:complexType>  </xs:element></xs:schema>

This is using C# in VS2005.I can create an instance of the XmlDocument and apply the schema to it, but im stumped if I can see how to populate the xmldocument using the structure predefined by the schema.The code I have so far is this:

		XmlReaderSettings schemaIn;		XmlDocument docOut;		public Form1()		{			InitializeComponent();		}		private void button1_Click(object sender, EventArgs e)		{			docOut = new XmlDocument();			schemaIn = new XmlReaderSettings();			schemaIn.Schemas.Add("trySchema", "schemaFile.xsd");			schemaIn.Schemas.Compile();			docOut.Schemas = schemaIn.Schemas;	// then what?

The resulting xml should contain multiple entries of the value/speed/time element.My guess is that there would be a loop which reads my raw data, creates an element, and populates it, something like this:

while (readingFile){	read(file, record);	create(docOut, element);		write(docOut, element , record.value);	write(docOut, element , record.speed);	write(docOut, element , record.time);}docOut.Save("filename");

Ive had a look at the DOM XML and Schema tutorials, but cant quite see the solution.

Link to comment
Share on other sites

Yeah. You'll have to manually loop over your binary data and create the document. The only thing you could use the Schema for (in this case) is to check if the resulting file matches your expectations.XmlDocument contains methods like "CreateElement", "CreateAttribute", etc. which create different node types.Once created like this (and stored in a variable), elements may be appended to the correct node by going to the node, and using its "appendChild" method. For example:

while (readingFile){	read(file, record);	XmlElement logResults = docOut.CreateElement("logResults");		logResults.SetAttribute("value", record.value);		logResults.SetAttribute("speed", record.speed);		logResults.SetAttribute("time", record.time);	docOut.DocumentElement.AppendChild(logResults);}docOut.Save("filename");

However, you'll also need to have another element as a root element. Just looking at your Schema, you can see that it only allows one element and that's all. You need to create another element, in which you allow multiple of those elements. Also, in DOM, you need to manually create, and append this element to the "docOut".

Link to comment
Share on other sites

Thanks for the info, the code snippet for CreateElement is what I had been looking for. I was trying to use an XmlNode.

You need to create another element, in which you allow multiple of those elements. Also, in DOM, you need to manually create, and append this element to the "docOut".
I reckon Ive got the other element in ok, as the schema states theres a 'sequence'. Having run it through the debugger, I think I see a problem. Im not loading the schema properly as examining the value of 'schemaIn' after it does the compile reveals that it contains 0 elements, as when I come to do the AppendChild, it raises a NullReferenceException.Maybe im missing doing as you suggested, to manually create and append this element?Not quite sure. As you can probably tell im new to xml, but appreciate all the help.Heres the updated schema:
<?xml version="1.0" encoding="utf-8"?><xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">  <xs:complexType name="logResultsCT">	<xs:sequence>	  <xs:element name="logResults">		<xs:complexType>		  <xs:attribute name="value" type="xs:decimal" />		  <xs:attribute name="speed" type="xs:unsignedShort" />		  <xs:attribute name="time" type="xs:int" />		</xs:complexType>	  </xs:element>	</xs:sequence>  </xs:complexType></xs:schema>

and I can see the zero element count after the compile in this:

			docOut = new XmlDocument();			schemaIn = new XmlReaderSettings();			schemaIn.Schemas.Add("trySchema", "schemaFile.xsd");			schemaIn.Schemas.Compile();

Link to comment
Share on other sites

XML documents MUST contain ONE, and ONLY ONE root element, in which there can be various elements. This root element, and everything in it may be constrained with XML Schema, but the presence of a root element is still a must.The complexType "logResultsCT" you've defined is appropriate for usage on an element. The problem is there is no element to apply it on.For example, you could add this (as a child to xs:schema):

<xs:element name="logResultsSet" type="logResultsCT" />

This declares that our XML may contain an element named "logResultsSet" of type "logResultsCT". A type that we've declared may contain a sequence of 1 "logResults" elements. Why 1 you ask? It's what's assumed when you don't have occurance indicators. Use "unbounded" on maxOccurs to make the maximum number of such elements unlimited.Anyway, DOM won't add the element on its own based on the schema. You must still add it in the same fashion as before, only doing so on the "docOut" instead of "docOut.DocumentElement", like:

			docOut = new XmlDocument();			docOut.AppendChild(docOut.CreateElement("logResultsSet"));

BTW, docOut.DocumentElement is actually a reference to this very element. Your NullReferenceException is so exactly because there was no such element. With the above, the exception should be gone.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...