4

I need to generate 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 have read these questions:

I am using Xsd2Code to generate the classes from XSD. That is working.

I have 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 it 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 to?

Update

I have also already tried this using XmlSerializerNamespaces. If I add namespaces like this, using my implementation above with the necessary adjustments:

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 namespaces like this, using my implementation above with the necessary adjustments:

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 my desired output? I really don't want to use StringBuilder for this task...

Community
  • 1
  • 1
Adriano Carneiro
  • 57,693
  • 12
  • 90
  • 123

3 Answers3

2

You can do as you did up there with:

XmlSerializerNamespaces namespaces = 
new XmlSerializerNamespaces(new[] { new XmlQualifiedName("ns","some.domain.api") });

and after writing the file do this:

StreamReader reader = new StreamReader(file_name);
string content = reader.ReadToEnd();
reader.Close();
content = Regex.Replace(content, ":ns", "");
StreamWriter writer = new StreamWriter(file_name);
writer.Write(content);
writer.Close();

Not the most elegant, but it works.

coldandtired
  • 993
  • 1
  • 10
  • 13
2

The trick is to use an empty name for the namespace:

XmlSerializerNamespaces namespaces = 
new XmlSerializerNamespaces(new[] { new XmlQualifiedName("","some.domain.api") });

Then you add them for the serialization process:

serializer.Serialize(writer, obj, namespaces);

Thats all to omit the "ns:".

Christian
  • 1,017
  • 3
  • 14
  • 30
0

try something like this:

 public static string SerializeObject(object obj)
    {
        XmlSerializerNamespaces XSN = new XmlSerializerNamespaces();
        XSN.Add("bs", "some.domain.api");

        XmlWriterSettings XWS = new XmlWriterSettings();
        XWS.OmitXmlDeclaration = true;

        Stream stream = new MemoryStream();
        XmlTextWriter xtWriter = new XmlTextWriter(stream, new UTF8Encoding(false));

        XmlSerializer ser = new XmlSerializer(obj.GetType());
        ser.Serialize(xtWriter, obj, XSN);

        xtWriter.Flush();

        stream.Seek(0, System.IO.SeekOrigin.Begin);
        string xml = Encoding.UTF8.GetString(((MemoryStream)stream).ToArray());

        return xml;
    }

updated

You can always replace the returning xml string "xmlns:bs" to "xmlns" if you are forced to a fixed xml output syntax!

danyolgiax
  • 12,798
  • 10
  • 65
  • 116