Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Small Question on XML Serialization using ASP.NET C#

Posted on 2009-05-02
7
Medium Priority
?
578 Views
Last Modified: 2012-05-06
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

0
Comment
Question by:meetpd
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
7 Comments
 
LVL 96

Expert Comment

by:Bob Learned
ID: 24292205
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.
0
 
LVL 21

Expert Comment

by:MogalManic
ID: 24292243
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

0
 

Author Comment

by:meetpd
ID: 24306449
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!

0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 21

Expert Comment

by:MogalManic
ID: 24306708
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-->



0
 

Author Comment

by:meetpd
ID: 24306926
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!
0
 
LVL 21

Accepted Solution

by:
MogalManic earned 2000 total points
ID: 24307364
It should.  Try it out.

This is from the documentation:http://msdn.microsoft.com/en-us/library/kw0f5wee(VS.80).aspx

defaultNamespace
    The default namespace to use for all the XML elements.
0
 

Author Comment

by:meetpd
ID: 24344603
Yes, it worked!! Thanks for solving my problem.
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
The Confluence of Individual Knowledge and the Collective Intelligence At this writing (summer 2013) the term API (http://dictionary.reference.com/browse/API?s=t) has made its way into the popular lexicon of the English language.  A few years ago, …
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…

730 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question