0

I usually search the web high and low for my answers but this time I am drawing a blank. I'm using VS2005 to write code to POST xml to an API. I have classes setup in C# that I serialize into an XML document. The classes are below:

[Serializable]
    [XmlRoot(Namespace = "", IsNullable = false)]
    public class Request
    {
        public RequestIdentify Identify;

        public string Method;

        public string Params;

    }

    [Serializable]
    public class RequestIdentify
    {
        public string StoreId;

        public string Password;
    }

When I serialize this I get:

<?xml version="1.0" encoding="UTF-8"?>
<Request xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
   <Identify>
      <StoreId>00</StoreId>
      <Password>removed for security</Password>
   </Identify>
   <Method>ProductExport</Method>
   <Params />
</Request>

But the API returns a "No XML sent" error.

If I send the xml directly in a string as:

string xml = @"<Request><Identify><StoreId>00</StoreId><Password>Removed for security</Password></Identify><Method>ProductExport</Method><Params /></Request>";

effectively sending this xml (without the schema info in the "Request" tag):

<Request>
   <Identify>
      <StoreId>00</StoreId>
      <Password>Removed for security</Password>
   </Identify>
   <Method>ProductExport</Method>
   <Params />
</Request>

It seems to recognise the XML no problem.

So my question I guess is how can I change my current classes to Serialize into XML and get the XML as in the second case? I assume I need another "parent" class to wrap around the existing ones and use InnerXml property on this "parent" or something similar but I don't know how to do this.

Apologies for the question, I've only been using C# for 3 months and I'm a trainee developer who is having to teach himself on the job!

Oh and PS I don't know why but VS2005 really does not want to let me set these classes up with private variables and then use getters and setters on public equivalents so I have them written how they are for now.

Thanks in advance :-)

UPDATE:

As with most things it's very hard to find answers if you're not sure what you need to ask or how to word it but:

Once I knew what to look for I found the answers I needed:

Removing the XML declaration:

XmlWriterSettings writerSettings = new XmlWriterSettings();
writerSettings.OmitXmlDeclaration = true;
StringWriter stringWriter = new StringWriter();
using (XmlWriter xmlWriter = XmlWriter.Create(stringWriter, writerSettings))
{
    serializer.Serialize(xmlWriter, request);
}
string xmlText = stringWriter.ToString();

Removing/Setting the namespace (Thanks to above replies that helped find this one!):

XmlSerializerNamespaces ns = new XmlSerializerNamespaces();
ns.Add("", "");

Thanks for your help everyone who answered or pointed me in the right direction! And yes I did find articles to read once I knew what I was asking :-) It's the first time I have come unstuck in 3 months of teaching myself so I think I'm doing pretty well...

Hadley
  • 1
  • 2
  • 1
    Possible duplicate of [XmlSerializer: remove unnecessary xsi and xsd namespaces](http://stackoverflow.com/questions/760262/xmlserializer-remove-unnecessary-xsi-and-xsd-namespaces) – J0HN Jun 26 '12 at 12:56
  • NOT A SOLUTION but you can try manipulating the string xml by replacing the namespace attributes with empty string. – Furqan Hameedi Jun 26 '12 at 12:57
  • 1
    Please show how you serialize your classes. My guess you need `XmlSerializerNamespaces _namespaces = new XmlSerializerNamespaces(new[] { XmlQualifiedName.Empty }); serializer.Serialize(writer, data, _namespaces);` – Renatas M. Jun 26 '12 at 12:57

1 Answers1

0

From Rydal's blog:

The XmlDocument object by default assigns a namespace to the XML string and also includes the declaration as the first line of the XML document. I definitely do not need or use those, therefore, I need to remove them. Here is how you go about doing just that.

Removing declaration and namespaces from XML serialization

Ilmo Euro
  • 4,925
  • 1
  • 27
  • 29
  • I added: ` XmlSerializerNamespaces ns = new XmlSerializerNamespaces(); ns.Add(string.Empty, string.Empty);` and it now removes the unwanted namespaces but the `` seems to be still causing an issue. I will dig around to see how to remove this too. – Hadley Jun 26 '12 at 13:16
  • Did you set `OmitXmlDeclaration` of `XmlWriterSettings` to true? – Ilmo Euro Jun 26 '12 at 13:30
  • Cant you just follow article? `Removing the XML declaration` section is about that – Renatas M. Jun 26 '12 at 13:31
  • Yes sorry I see that article now, it wouldn't open for me before but thanks. I appreciate your patience with me :-) – Hadley Jun 26 '12 at 13:40