Link to home
Start Free TrialLog in
Avatar of meetpd
meetpdFlag for India

asked on

Small Question on XML Serialization using ASP.NET C#

In shown in Code below, I have an XML file that I need to generate using XML Schema thru XML Serialzation.

In this XML file, there is a prefix 'Shop:' being used throughout all the nodes. This Prefix is not present in the Class File that I generated using XSD.

How to I add this prefix in all the nodes of the XML file that I generate?

I am using MS ASP.NET C# to achieve this.

Thanks!
<Shop:StoreInfo>
        <Shop:ItemName>
                    <Shop:ProductCode>String</Shop:ProductCode>
                     <Shop:ProductType>String</Shop:ProductType>
        </Shop:ItemName>
</Shop:StoreInfo>

Open in new window

Avatar of Bob Learned
Bob Learned
Flag of United States of America image

Shop: would normally be associated with a namespace.  If you don't have a namespace, I believe that you should be able to generate a namespace in order to get that prefix.
You need to serialize with the XmlSerializerNamespaces class and use the XmlNamespace attribute on your class:
http://msdn.microsoft.com/en-us/library/system.xml.serialization.xmlserializernamespaces(VS.80).aspx

So the code would be something like this:

public class Run
{
   public static void Main()
   {
      Run test = new Run();
      test.SerializeObject("XmlNamespaces.xml");
   }
 
   public void SerializeObject(string filename)
   {
      XmlSerializer s = new XmlSerializer(typeof(Books));
      // Writing a file requires a TextWriter.
      TextWriter t = new StreamWriter(filename);
 
      /* Create an XmlSerializerNamespaces object and add two
      prefix-namespace pairs. */
      XmlSerializerNamespaces ns = new XmlSerializerNamespaces();
      ns.Add("Shop", "http://www.MyCompanyName.Com/ShopXml");
 
      // Create a ShopInfo instance.
      ShopInfo shp = new ShopInfo();
      /*...Setup Info class.../*
 
      s.Serialize(t,shp,ns);
      t.Close();
   }
}
//...
[XmlType(Namespace ="http://www.MyCompanyName.Com/ShopXml")]
public class ShopInfo
{
//...The rest of class definition...
}

Open in new window

Avatar of meetpd

ASKER

Thanks for the sample code.

But this code changes the name of the namespace. What we need here to add prefix to all class names so that ALL nodes will have prefix 'Shop:'.

Can you please suggest something?

Thanks!

If you want the default namespace to be the "Shop" namespace, then just change the XmlSerializer constructor (line 11 in above code) to:
      XmlSerializer s = new XmlSerializer(typeof(ShopInfo), "http://www.MyCompanyName.Com/ShopXml");


This will produce XML something like this:
<?xml version="1.0" encoding="utf-16"?>

  ShopName
  <!--Other XML nodes-->



Avatar of meetpd

ASKER

Thanks...but will this also create 'Shop:' as prefix for ALL nodes as given below?

 <Shop:ItemName>
                    <Shop:ProductCode>String</Shop:ProductCode>
                     <Shop:ProductType>String</Shop:ProductType>
        </Shop:ItemName>

Really appreciate your quick response!
ASKER CERTIFIED SOLUTION
Avatar of MogalManic
MogalManic
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of meetpd

ASKER

Yes, it worked!! Thanks for solving my problem.