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="" xmlns:cm="" xmlns:deccm=""
xmlns:deckey="" xmlns:sau="" xmlns:xsi=""
xsi:schemaLocation="" sau:docType="Text" sau:id="A0" sau:msgType="Text" decsub:docType="DEC" decsub:id="A0" decsub:msgType="H2HDECSUB">
            <sau:payload xsi:type="decsub:declarationSubInfoType">
                        <decsub:regPort cm:type="0">Str</decsub:regPort>

And it comes like this

<saudiEDI xmlns:xsi="" xmlns:xsd="" d1p1:docType="DEC" d1p1:id="AN20090107120342" d1p1:msgType="H2HDECSUB" xmlns:d1p1="" xmlns="">
    <d1p1:payload xsi:type="declarationSubInfoType">
        <regPort d5p1:type="4" xmlns:d5p1="" />
Niyas MaraicarAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

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?
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.
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);

In samplexml.cs file

these are the attributes added to the class

[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.6.1055.0")]
[System.Xml.Serialization.XmlRootAttribute("saudiEDI", Namespace="", IsNullable=false)]

but instead of this result

<decsub:saudiEDI .....>

the output is like this


Hope this helps to solve this problem..
Introduction to R

R is considered the predominant language for data scientist and statisticians. Learn how to use R for your own data science projects.

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,
before the classes,

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

I need to add the XMLSerializerNamespace.

 XmlSerializerNamespaces xmlNameSpace = new XmlSerializerNamespaces();
            xmlNameSpace.Add("decsub", "");
            xmlNameSpace.Add("cm", "");

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
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

From novice to tech pro — start learning today.