How to create a XML document with namespaces using XmlSerializer class ? C#

I have a XSD Schema and I have created C# classes. When  I tried to create an XML document using XmlSerializer class, the classes are created in such a way that  instead of namespaces in XML document, the namespaces are added as attributes.

I want to create like this

<decsub:saudiEDI xmlns:decsub="http://www.saudiedi.com/schema/decsub" xmlns:cm="http://www.saudiedi.com/schema/common" xmlns:deccm="http://www.saudiedi.com/schema/deccm"
xmlns:deckey="http://www.saudiedi.com/schema/deckey" xmlns:sau="http://www.saudiedi.com/schema/sau" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.saudiedi.com/schema/decsub.xsd" sau:docType="Text" sau:id="A0" sau:msgType="Text" decsub:docType="DEC" decsub:id="A0" decsub:msgType="H2HDECSUB">
      <decsub:record>
            <sau:payload xsi:type="decsub:declarationSubInfoType">
                  <decsub:reference>
                        <decsub:userid>String</decsub:userid>
                        <decsub:acctId>Stri</decsub:acctId>
                        <decsub:docRefNo>String</decsub:docRefNo>
                        <decsub:regPort cm:type="0">Str</decsub:regPort>
                        <decsub:declarationISN>9999999999</decsub:declarationISN>
                  </decsub:reference>
.......


And it comes like this

<saudiEDI xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" d1p1:docType="DEC" d1p1:id="AN20090107120342" d1p1:msgType="H2HDECSUB" xmlns:d1p1="http://www.saudiedi.com/schema/sau" xmlns="http://www.saudiedi.com/schema/decsub">
  <d1p1:record>
    <d1p1:payload xsi:type="declarationSubInfoType">
      <reference>
        <userid>abc001</userid>
        <acctId>1234</acctId>
        <docRefNo>12345678901234</docRefNo>
        <regPort d5p1:type="4" xmlns:d5p1="http://www.saudiedi.com/schema/common" />
      </reference>
      <senderInformati
Niyas MaraicarAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Flabio GatesCommented:
Would you care to share what code you have so far?
0
ste5anSenior DeveloperCommented:
Please post concise and complete examples. And embed code into [code][/code] tags (the  CODE button).

When posting questions about XML, it is good style to post complete, well-formed and valid XML. Yours are invalid. E.g. xsi:schemaLocation is wrongly declared in both snippets. Also post the XSD for such questions (e.g. as attachment).

When the used sample is considered not for public, then craft a sample showing your behavior. Benefit: You'll learn a lot about XML and coding.

And last but not least: As it involves .NET, also post a concise and complete sample of your XML creation.
0
Niyas MaraicarAuthor Commented:
Thanks Mr.Gates and Mr.ste5an.

PFA xml document and the class created by xsd.

And here is my code to create the XML document.

           //initializing obj with values
 objSaudiEdiDecSubType.docType = "string";

          XmlSerializer serializer = new XmlSerializer(typeof(saudiediDecSubType));
           
            serializer.Serialize(writer, objSaudiEdiDecSubType);
            writer.Close();

In samplexml.cs file

these are the attributes added to the class

[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.6.1055.0")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(Namespace="http://www.saudiedi.com/schema/sdcsub")]
[System.Xml.Serialization.XmlRootAttribute("saudiEDI", Namespace="http://www.saudiedi.com/schema/sdcsub", IsNullable=false)]

but instead of this result

<decsub:saudiEDI .....>


the output is like this

<saudiEDI....>

Hope this helps to solve this problem..
samplexml.cs
samplexmlfile.xml
0
Cloud Class® Course: Microsoft Windows 7 Basic

This introductory course to Windows 7 environment will teach you about working with the Windows operating system. You will learn about basic functions including start menu; the desktop; managing files, folders, and libraries.

Niyas MaraicarAuthor Commented:
I just want to simplify my question.

I just want to create an XML document with namespaces using C#.

Suggest me how to achieve..

If I add the attributes,
[System.Xml.Serialization.XmlTypeAttribute(Namespace="http://www.saudiedi.com/schema/sdcsub")]
before the classes,

the XML element contains attributes.
But I want the namespaces to be added to the XML element
0
Niyas MaraicarAuthor Commented:
I got the solution

I need to add the XMLSerializerNamespace.

 XmlSerializerNamespaces xmlNameSpace = new XmlSerializerNamespaces();
            xmlNameSpace.Add("decsub", "http://www.saudiedi.com/schema/decsub");
            xmlNameSpace.Add("cm", "http://www.saudiedi.com/schema/common");
1

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Niyas MaraicarAuthor Commented:
this is the solution to my problem
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
XSD

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.