I need to create the following XML:
<Learner xmlns="some.domain.api"> <ActivationDate>1999-05-31T11:20:00</ActivationDate> <EmailAddress>String content</EmailAddress> <ExpirationDate>1999-05-31T11:20:00</ExpirationDate> <FederalId>String content</FederalId> <FirstName>String content</FirstName> <Grade>K</Grade> <LastName>String content</LastName> <MiddleName>String content</MiddleName> <UserName>String content</UserName> <Password>String content</Password> <SISId>String content</SISId> <StateId>String content</StateId> <Status>Active</Status> </Learner>
I read the following questions:
I use Xsd2Code to create classes from XSD. It works .
I tried this:
private static string SerializeXML(Object obj) { XmlSerializer serializer = new XmlSerializer(obj.GetType(), "some.domain.api"); //settings XmlWriterSettings settings = new XmlWriterSettings(); settings.Indent = true; settings.OmitXmlDeclaration = true; StringWriter stream = new StringWriter(); XmlWriter writer = XmlWriter.Create(stream, settings); serializer.Serialize(writer, obj); return stream.ToString(); }
But he produces this:
<Learner xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="some.domain.api"> <ActivationDate>1999-05-31T11:20:00</ActivationDate> <EmailAddress>String content</EmailAddress> <ExpirationDate>1999-05-31T11:20:00</ExpirationDate> <FederalId>String content</FederalId> <FirstName>String content</FirstName> <Grade>K</Grade> <LastName>String content</LastName> <MiddleName>String content</MiddleName> <UserName>String content</UserName> <Password>String content</Password> <SISId>String content</SISId> <StateId>String content</StateId> <Status>Active</Status> </Learner>
I want to get rid of xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" .
How?
Update
I have already tried this using XmlSerializerNamespaces . If I add such namespaces using my implementation above with the necessary settings:
XmlSerializerNamespaces namespaces = new XmlSerializerNamespaces(new[] { new XmlQualifiedName("ns","some.domain.api") });
I get this:
<Learner xmlns:ns="some.domain.api">
But I need this:
<Learner xmlns="some.domain.api">
If I add such namespaces using my implementation above with the necessary settings:
XmlSerializerNamespaces namespaces = new XmlSerializerNamespaces(new[] { XmlQualifiedName.Empty });
I get this:
<q1:Learner xmlns:ns="some.domain.api">
for all XML tags!
Is there any way I can do to get the desired result? I really don't want to use StringBuilder for this task ...