V4mp1r3

Hello

I'm trying to add an attribute to each element of a XML Schema, so i'm doing it this way:

Dim MyStringWriter As New StringWriter

MyDataset.WriteXmlSchema(MyStringWriter)

Dim MyStringReader As StringReader = New StringReader(mystringwriter.ToString)

Dim MyXMLSchema As New Schema.XmlSchema

Dim MyXMLValidationHandler As Schema.ValidationEventHandler = New Schema.ValidationEventHandler(AddressOf ValidationEventHandler)

MyXMLSchema.Read(MyStringReader, MyXMLValidationHandler)

For Each element As Schema.XmlSchemaElement In MyXMLSchema.Elements

element.IsNillable = True

Next

The problem is that when i read the XMLSchema into the MyXMLSchema object, it's giving me "The root element is missing"

I have this XML schema:

< xml version="1.0" encoding="utf-16" >
<xsTongue Tiedchema id="NewDataSet" xmlns="" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urnTongue Tiedchemas-microsoft-com:xml-msdata">
<xs:element name="NewDataSet" msdata:IsDataSet="true" msdata:Locale="pt-PT">
<xs:complexType>
<xs:choice maxOccurs="unbounded">
<xs:element name="Table">
<xs:complexType>
<xsTongue Tiedequence>
<xs:element name="Distritos" type="xsTongue Tiedtring" minOccurs="0" />
<xs:element name="F2" type="xsTongue Tiedtring" minOccurs="0" />
<xs:element name="F3" type="xsTongue Tiedtring" minOccurs="0" />
<xs:element name="F4" type="xsBig Smileouble" minOccurs="0" />
<xs:element name="F5" type="xsBig Smileouble" minOccurs="0" />
<xs:element name="F6" type="xsBig Smileouble" minOccurs="0" />
<xs:element name="F7" type="xsTongue Tiedtring" minOccurs="0" />
<xs:element name="F8" type="xsTongue Tiedtring" minOccurs="0" />
<xs:element name="F9" type="xsTongue Tiedtring" minOccurs="0" />
<xs:element name="F10" type="xsTongue Tiedtring" minOccurs="0" />
<xs:element name="F11" type="xsTongue Tiedtring" minOccurs="0" />
<xs:element name="F12" type="xsTongue Tiedtring" minOccurs="0" />
<xs:element name="F13" type="xsTongue Tiedtring" minOccurs="0" />
<xs:element name="F14" type="xsTongue Tiedtring" minOccurs="0" />
<xs:element name="F15" type="xsTongue Tiedtring" minOccurs="0" />
<xs:element name="F16" type="xsTongue Tiedtring" minOccurs="0" />
<xs:element name="F17" type="xsTongue Tiedtring" minOccurs="0" />
<xs:element name="F18" type="xsTongue Tiedtring" minOccurs="0" />
<xs:element name="F19" type="xsTongue Tiedtring" minOccurs="0" />
</xsTongue Tiedequence>
</xs:complexType>
</xs:element>
</xs:choice>
</xs:complexType>
</xs:element>
</xsTongue Tiedchema>



Re: XML and the .NET Framework Root element is missing ( Urgent )

Martin Honnen

The error message "the root element is missing" sounds as if you try to parse a string that does not contain an element at all. Make sure you are really passing a string with XML markup to the method.




Re: XML and the .NET Framework Root element is missing ( Urgent )

V4mp1r3

The XML Schema i wrote is the result from MyStringReader, so i dont parse an empty string.



Re: XML and the .NET Framework Root element is missing ( Urgent )

Martin Honnen

The Read method is a static method so try

Code Snippet
Dim MyXmlSchema As XmlSchema = XmlSchema.Read(MyStringReader, _
MyXmlValidationEventHandler)






Re: XML and the .NET Framework Root element is missing ( Urgent )

V4mp1r3

Didn't worked..:\ i'm starting to be worried lol Smile thanks for your help



Re: XML and the .NET Framework Root element is missing ( Urgent )

V4mp1r3

Any ideias



Re: XML and the .NET Framework Root element is missing ( Urgent )

Martin Honnen

What exactly "does not work" now Do you still get the error "root element is missing" Please show your current code.




Re: XML and the .NET Framework Root element is missing ( Urgent )

V4mp1r3

Yes i'm still getting the same error.

Code Snippet

Dim MyStringWriter As New StringWriter

MyDatasetXLS.WriteXmlSchema(MyStringWriter)

Dim MyStringReader As StringReader = New StringReader(MyStringWriter.ToString)

Dim MyXMLValidationHandler As Schema.ValidationEventHandler = New Schema.ValidationEventHandler(AddressOf ValidationEventHandler)

Dim MyXmlSchema As Schema.XmlSchema = Schema.XmlSchema.Read(MyStringReader, MyXMLValidationHandler)

For Each element As Schema.XmlSchemaElement In MyXmlSchema.Elements

element.IsNillable = True

Next

MyStringReader.Close()

MyStringWriter.Flush()

MyStringWriter.Close()





Re: XML and the .NET Framework Root element is missing ( Urgent )

Martin Honnen

For which line/statement do you get the error




Re: XML and the .NET Framework Root element is missing ( Urgent )

V4mp1r3

I'm getting the error here:

Code Snippet

Dim MyXmlSchema As Schema.XmlSchema = Schema.XmlSchema.Read(MyStringReader, MyXMLValidationHandler)





Re: XML and the .NET Framework Root element is missing ( Urgent )

Martin Honnen

Let's try to break that code up so that we can find where the problem comes from:

Code Snippet

Dim MyStringWriter As New StringWriter

MyDatasetXLS.WriteXmlSchema(MyStringWriter)

Dim Xsd As String = MyStringWriter.ToString

' put a Console.WriteLine(Xsd)

' or MsgBox(Xsd) here to check

' that the string contains a schema

Dim MyStringReader As StringReader = New StringReader(Xsd)

Dim MyXMLValidationHandler As Schema.ValidationEventHandler = New Schema.ValidationEventHandler(AddressOf ValidationEventHandler)

Dim MyXmlSchema As Schema.XmlSchema = Schema.XmlSchema.Read(MyStringReader, MyXMLValidationHandler)

then tell use what the Control.WriteLine or MsgBox shows.






Re: XML and the .NET Framework Root element is missing ( Urgent )

V4mp1r3

It returns this schema :

Code Snippet

< xml version="1.0" encoding="utf-16" >

<xs:schema id="NewDataSet" xmlns="" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">

<xs:element name="NewDataSet" msdata:IsDataSet="true" msdata:Locale="pt-PT">

<xs:complexType>

<xs:choice maxOccurs="unbounded">

<xs:element name="Table">

<xs:complexType>

<xs:sequence>

<xs:element name="Distritos" type="xs:string" minOccurs="0" />

<xs:element name="F2" type="xs:string" minOccurs="0" />

<xs:element name="F3" type="xs:string" minOccurs="0" />

<xs:element name="F4" type="xs:double" minOccurs="0" />

<xs:element name="F5" type="xs:double" minOccurs="0" />

<xs:element name="F6" type="xs:double" minOccurs="0" />

<xs:element name="F7" type="xs:string" minOccurs="0" />

<xs:element name="F8" type="xs:string" minOccurs="0" />

<xs:element name="F9" type="xs:string" minOccurs="0" />

<xs:element name="F10" type="xs:string" minOccurs="0" />

<xs:element name="F11" type="xs:string" minOccurs="0" />

<xs:element name="F12" type="xs:string" minOccurs="0" />

<xs:element name="F13" type="xs:string" minOccurs="0" />

<xs:element name="F14" type="xs:string" minOccurs="0" />

<xs:element name="F15" type="xs:string" minOccurs="0" />

<xs:element name="F16" type="xs:string" minOccurs="0" />

<xs:element name="F17" type="xs:string" minOccurs="0" />

<xs:element name="F18" type="xs:string" minOccurs="0" />

<xs:element name="F19" type="xs:string" minOccurs="0" />

</xs:sequence>

</xs:complexType>

</xs:element>

</xs:choice>

</xs:complexType>

</xs:element>

</xs:schema>





Re: XML and the .NET Framework Root element is missing ( Urgent )

Martin Honnen

Sorry, I am not able to find why you get that error about a missing root element. I have taken your schema and loaded it in a data set, then wrote that out again, that works flawlessly for me:

Code Snippet

Imports System

Imports System.Data

Imports System.IO

Imports System.Xml.Schema

Module Module1

Sub Main()

Dim MyDataSet As DataSet = New DataSet()

MyDataSet.ReadXmlSchema("..\..\XSDSchema1.xsd")

Dim MyStringWriter As New StringWriter

MyDataSet.WriteXmlSchema(MyStringWriter)

Dim MyStringReader As StringReader = New StringReader(MyStringWriter.ToString)

Dim MyXMLSchema As New XmlSchema

MyXMLSchema = XmlSchema.Read(MyStringReader, New ValidationEventHandler(AddressOf ValidationHandler))

MyXMLSchema.Write(Console.Out)

End Sub

Sub ValidationHandler(ByVal sender As Object, ByVal args As ValidationEventArgs)

Console.WriteLine(args.Message)

End Sub

End Module






Re: XML and the .NET Framework Root element is missing ( Urgent )

V4mp1r3

I dont get it also.. i could read the schema to a dataset, but i could not write it to the XMLSchema variable.. oh my god lol..

Thank you very much for your attention