Hello everyone,
My confusion is, I am using .Net C# XMLSerializer to serialize a customize defined type, using the schema/cs file generated by XSD tool from an input original XML file. But the generated serialized XML file namespace is different from original XML input file. Especially from original XML file, Envelope belongs to namespace soapenv, but in the serialized XML file, Envelope belongs to default namespace. Any ideas what is wrong?
Here is how I use XSD tool to generate schema file and cs file for the customized defined type from XML input file XMLFile1.xml,
BTW: XML files can be downloaded from,
http://www.mediafire.com/?nwngwzz3gmm
D:\>xsd XMLFile1.xml
Microsoft (R) Xml Schemas/DataTypes support utility
[Microsoft (R) .NET Framework, Version 2.0.50727.3038]
Copyright (C) Microsoft Corporation. All rights reserved.
Writing file 'D:\XMLFile1.xsd'.
D:\>xsd XMLFile1.xsd XMLFile1_app1.xsd /classes
Microsoft (R) Xml Schemas/DataTypes support utility
[Microsoft (R) .NET Framework, Version 2.0.50727.3038]
Copyright (C) Microsoft Corporation. All rights reserved.
Writing file 'D:\XMLFile1_XMLFile1_app1.cs'.
Here is how I use C# code to serialize the file and output to TestOutputFile.xml,
static void Main(string[] args)
{
Envelope en = new Envelope();
en.Items = new EnvelopeBody[1];
en.Items[0] = new EnvelopeBody();
en.Items[0].QueryResponse = new QueryResponseFaculties[1];
en.Items[0].QueryResponse[0] = new QueryResponseFaculties();
en.Items[0].QueryResponse[0].Name = "John";
XmlSerializer serializer =
new XmlSerializer(typeof(Envelope));
TextWriter writer = new StreamWriter("TestOutputFile.xml");
serializer.Serialize(writer, en);
writer.Close();
return;
}
The original XML file is,
<?xml version="1.0" encoding="utf-8"?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
<soapenv:Body>
<QueryResponse xmlns="http://schemas.mycorp.com/test">
<Faculties>
<Name>
John
</Name>
</Faculties>
</QueryResponse>
</soapenv:Body>
</soapenv:Envelope>
The serialized XML file is,
<?xml version="1.0" encoding="utf-8"?>
<Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns="http://schemas.xmlsoap.org/soap/envelope/">
<Body>
<QueryResponse xmlns="http://schemas.mycorp.com/test">
<Faculties>
<Name>John</Name>
</Faculties>
</QueryResponse>
</Body>
</Envelope>
EDIT 1:
Current code is,
static void Main(string[] args)
{
XmlSerializerNamespaces ns = new XmlSerializerNamespaces();
ns.Add("soapenv", "http://schemas.xmlsoap.org/soap/envelope/");
ns.Add("", "http://schemas.mycorp.com/test");
ns.Add("xsd", "http://www.w3.org/2001/XMLSchema");
ns.Add("xsi", "http://www.w3.org/2001/XMLSchema-instance");
Envelope en = new Envelope();
en.Items = new EnvelopeBody[1];
en.Items[0] = new EnvelopeBody();
en.Items[0].QueryResponse = new QueryResponseFaculties[1];
en.Items[0].QueryResponse[0] = new QueryResponseFaculties();
en.Items[0].QueryResponse[0].Name = "John";
XmlSerializer serializer =
new XmlSerializer(typeof(Envelope));
TextWriter writer = new StreamWriter("TestOutputFile.xml");
serializer.Serialize(writer, en, ns);
writer.Close();
return;
}
Current output (serialized XML file) is,
<?xml version="1.0" encoding="utf-8"?>
<soapenv:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://schemas.mycorp.com/test"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
<soapenv:Body>
<QueryResponse>
<Faculties>
<Name>John</Name>
</Faculties>
</QueryResponse>
</soapenv:Body>
</soapenv:Envelope>
I want the output to be (notice the location of string xmlns="http://schemas.mycorp.com/test"),
<?xml version="1.0" encoding="utf-8"?>
<soapenv:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
<soapenv:Body>
<QueryResponse xmlns="http://schemas.mycorp.com/test">
<Faculties>
<Name>John</Name>
</Faculties>
</QueryResponse>
</soapenv:Body>
</soapenv:Envelope>
thanks in advance, George