Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8081483
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T16:44:28+00:00 2026-06-05T16:44:28+00:00

I am using an XSD file to generate C# code through xsd.exe and serializing

  • 0

I am using an XSD file to generate C# code through xsd.exe and serializing XML using the resulting code.

The problem is that I must generate an XML file having a defaut/root xmlns namespace, and an element definining another “raw” namespace on this element (without prefix).

Here is a light sample (not the whole XML), for illustration purposes :

<?xml version="1.0" encoding="utf-16"?>
<Request xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://mynamespace">
   <ABC>STACKOVERFLOW</ABC>
   <Method Name="TEST" xmlns="http://myothernamespace">
     <DEF>123456</DEF>
   </Method>
 </Request>

I can’t find a way to “insert” the second xmlns attribute as such on the Method element (without prefix).

Thing is that I cannot use prefix, as this XML is sent to an application that is not using an XML deserializer on the other end, but which is parsing the XML as a string and looking for exact string match (and of course this is not possible to change this “receiving” application).

I have tried a lot of different things in XSD but nothing works 🙁 I am using MSMQ to transfer the XML message so I am not serializing the Request by myself (just giving correct parameters to the request object and the serialization is handled behind the scene).

I could use an XmlSerializer to serialize the XML to a string without the second xmlns, and add it via coding to the XML (to the string) before sending it, but this is dirty and bugs me to go this way just to add this “xmlns” attribute on Method element.

Is there any way to go arround this via pure XSD (through .NET code generation using xsd.exe) or not ?

Hope my question is clear and hope some people will be able to answer it

Thanks in advance !

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T16:44:29+00:00Added an answer on June 5, 2026 at 4:44 pm

    Define your types like this:

    [XmlRoot("Method")]
    public class MyMethod
    {
        [XmlAttribute]
        public String Name   { get; set; }
        [XmlElement]
        public int DEF   { get; set; }
    }
    
    
    [XmlRoot("Request", Namespace="http://mynamespace")]
    public class MyRequest
    {
        [XmlElement]
        public String ABC { get; set; }
    
        [XmlElement(Namespace="http://myothernamespace")]
        public MyMethod Method { get; set; }
    }
    

    Supporting code:

        static TextWriter GetWriter(bool wantSave)
        {
            if (wantSave)
            {
                var fs = new FileStream(StorageFile, FileMode.Create);
                return new StreamWriter(fs, new UTF8Encoding());
            }
            return Console.Out;
        }
    
        private static void ShoworSave(MyRequest r, bool wantSave)
        {
            if (r==null)
            {
                Console.WriteLine(" --null--");
                return;
            }
    
            Console.WriteLine("\n");
    
            var writerSettings = new XmlWriterSettings
            {
                OmitXmlDeclaration = true,
                Indent = true
            };
    
            using (XmlWriter xmlWriter =
                   XmlWriter.Create(GetWriter(wantSave), writerSettings))
            {
                XmlSerializer ser = new XmlSerializer(r.GetType());
                var ns = new XmlSerializerNamespaces();
                ns.Add("", "http://mynamespace"); // default xmlns
                ser.Serialize(xmlWriter, r, ns);
            }
            Console.WriteLine("\n");
        }
    

    And then use like this:

            var request = new MyRequest
            {
                ABC = "HelloWorld",
                Method = new MyMethod
                {
                    Name="TEST",
                    DEF=123456
                }
            };
    
            SaveOrShow(request, false);
    

    results:

    <Request xmlns="http://mynamespace">
      <ABC>HelloWorld</ABC>
      <Method Name="TEST" xmlns="http://myothernamespace">
        <DEF>123456</DEF>
      </Method>
    </Request>
    

    Discussion

    The Xml serializer allows you to specify a namespace map, whereby you can give a list of namespace prefixes and the actual namespaces they map to, for the serialized output. To set the default xml namespace and its prefix, use the prefix of “” (empty string).

    So the code I use specifies that default namespace.

    I have also decorated the various types and members with the appropriate xml serializer attributes to get the right namespaces.

    At first glance, you may think that the use of the xml namespace string (in your example of “http://mynamespace”) in several distinct places in the code is a violation of “Dont repeat yourself” aphorism of clean coding. But this is not really true. In the one place I use it, it sets the XML namespace of the type. In the other place, it specifies the default xml namespace for the serializer.

    If I did not specify the latter then you would get a prefix; this would give you the semantically equivalent xml infoset, but because you said that your receiver application isn’t truly xml-aware, it would break that receiver.

    Also, regarding your question about xsd.exe and code generation, not sure what you’re getting at but consider this: Xsd.exe is just a tool. There’s nothing wrong with taking the output of that tool and tweaking it, editing it. If your types are relatively simple you may find it easier to just define the types in C#, as I have shown above. If you are heavy into XSD as source code, then you will want to rely on the xsd.exe tool. In that case you need to specify those namespaces in the proper places in the xsd document. Which option you choose is up to you.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have some Java code that validates XML against an XSD. I am using
I have this Maven task to generate Java classes from an XSD file using
I am new to xml's. I have used xsd.exe to create a .xsd file
In our project we are using Xsd2Code to generate c# code. The XSD I
I've created an .xsd file and then used XSD.exe to generate the .cs file
I am working on generating Java objects from an XSD file using JAXB 2.1.
Im using xsd:unique in an xsd generated code. How do I get an item
I'm new to XML validation using .xsd files. Maybe I'm not even asking the
I need to validate order of my output xml against a XSD using XMLUnit
I'm using Python's suds library which tries to fetch xml.xsd over the network. Unfortunately,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.