I am trying to convert xsd file to the .net class.
I have searched a lot for this topic and found xsd.exe as one of the way to achieve it, but I still have two concerns
-
I don’t want to manually generate the classes from command prompt but want all this to get done on runtime. For that I tried to use System.Diagnostics.Process to run xsd.exe on runtime but could not get succeeded and also was getting a blinking command prompt window when process starts.
-
I even didn’t get succeeded to get the classes generated from command prompt also. It is giving me error “Schema D:\Response.xsd could not be validated.”
So basically I am trying to implement something that will use my xsd string and deserialize it to one of my class’ type on runtime, something like we do with Xml string using XmlSerializer class.
I would like to mention that I think my xsd string is not of the general xsd types but kind of custom, an example of one of the responses is this
<?xml version="1.0" encoding="iso-8859-1"?>
<serv:message xmlns:serv="http://www.webex.com/schemas/2002/06/service"
xmlns:com="http://www.webex.com/schemas/2002/06/common"
xmlns:use="http://www.webex.com/schemas/2002/06/service/user">
<serv:header>
<serv:response>
<serv:result>SUCCESS</serv:result>
<serv:gsbStatus>BACKUP</serv:gsbStatus>
</serv:response>
</serv:header>
<serv:body>
<serv:bodyContent xsi:type="use:getLoginTicketResponse"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<use:ticket>5e9733eb9efeb02d80aa0551ef7e9ccd</use:ticket>
<use:apiVersion>WebEx XML API V3.9.0</use:apiVersion>
</serv:bodyContent>
</serv:body>
</serv:message>
Edit –
I got succeeded in generating the class file for my xml now and here it is
/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.0.30319.1")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(Namespace = "http://www.webex.com/schemas/2002/06/service/user")]
public partial class getLoginTicketResponse : bodyContentType
{
private string ticketField;
private string apiVersionField;
/// <remarks/>
public string ticket
{
get
{
return this.ticketField;
}
set
{
this.ticketField = value;
}
}
/// <remarks/>
public string apiVersion
{
get
{
return this.apiVersionField;
}
set
{
this.apiVersionField = value;
}
}
}
And this is bodyContentType
/// <remarks/>
[System.Xml.Serialization.XmlIncludeAttribute(typeof(getLoginTicketResponse))]
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.0.30319.1")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(Namespace = "http://www.webex.com/schemas/2002/06/service")]
[System.Xml.Serialization.XmlRootAttribute("getLoginTicket", Namespace = "http://www.webex.com/schemas/2002/06/service/user", IsNullable = false)]
public partial class bodyContentType
{
}
Now, trying to serialize it like this
var nameSpaceManager = new XmlNamespaceManager(responseXML.NameTable);
nameSpaceManager.AddNamespace("serv", "http://www.webex.com/schemas/2002/06/service");
nameSpaceManager.AddNamespace("com", "http://www.webex.com/schemas/2002/06/common");
nameSpaceManager.AddNamespace("xsi", "http://www.w3.org/2001/XMLSchema-instance");
nameSpaceManager.AddNamespace("meet", "http://www.webex.com/schemas/2002/06/service/meeting");
nameSpaceManager.AddNamespace("att", "http://www.webex.com/schemas/2002/06/service/attendee");
nameSpaceManager.AddNamespace("use", "http://www.webex.com/schemas/2002/06/service/user");
XmlNode statusNode = responseXML.SelectSingleNode("/serv:message/serv:body", nameSpaceManager);
TextReader reader = new StringReader(s);
XmlSerializer serializer = new XmlSerializer(typeof(getLoginTicketResponse));
var obj = serializer.Deserialize(reader);
But keep on getting the error
“There is an error in XML document (1, 2).”
I tried changing many things in the xml string, but could not get succeeded
Any help will be appreciable!
Got a very decent and easy approach to deserialize my xml using XDocument!!!! 🙂 🙂
Here is how I am doing it now
And I get my class object deserialized from the xml directly, no XSD nothing is required!
Let me show you my xml here again