473,382 Members | 1,247 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

How to remove xmlns:xsd at root tag via XmlSerialization

Hello,

I use Xml serialization to serialize an object into an xml file.

My root tag is defined as following:

[System.CodeDom.Compiler.GeneratedCodeAttribute("xs d",
"2.0.50727.42")]
[System.ComponentModel.DesignerCategoryAttribute("c ode")]
[SerializableAttribute()]
[XmlType("Style")]
public class RPStyle
{
//...
}

That will produce following xml:

<?xml version="1.0" encoding="utf-16"?>
<Style xmlns:xsd="http://www.w3.org/2001/XMLSchema-instance"/>

You see, that there is an attribute xmlns:xsd. How can I get rid of
that xmlns attribute?

Thank you,
Norbert
Jun 27 '08 #1
2 20331
Norbert Pürringer wrote:
<Style xmlns:xsd="http://www.w3.org/2001/XMLSchema-instance"/>

You see, that there is an attribute xmlns:xsd. How can I get rid of
that xmlns attribute?
Are you sure it is xmlns:xsd="http://www.w3.org/2001/XMLSchema-instance"
and not xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"?
That is the namespace declaration of the XMLSchema-instance namespace
and might be necessary to properly qualify xsi:type or xsi:nil
attributes during serialization.
--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/
Jun 27 '08 #2
Use the following code to get rid of the XML declaration and the namespace
declaration attributes:

// source: source object instance to serialize
// target: target file name to write the XML to
void Serialize(object source, string file)
{
XmlWriterSettings settings = new XmlWriterSettings();
settings.OmitXmlDeclaration = true;
settings.Indent = true;

using (XmlWriter writer = XmlWriter.Create(file, settings))
{
XmlSerializer serializer = new XmlSerializer(source.GetType());

XmlSerializerNamespaces namespaces = new XmlSerializerNamespaces();
namespaces.Add(string.Empty, string.Empty);

serializer.Serialize(writer, source, namespaces);
}
}

Jun 27 '08 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Kumar | last post by:
I have an XML file, for which I am trying to write a XSL style sheet. I have the following attributes defined in my XML file: xmlns xmlns:xsi xsi:schemaLocation numRecords How should I...
1
by: ksampson | last post by:
I simply want to generate an XML document. I want the root element to include an attribute like <rootElement xmlns:xsd="http://someurl"> .... </rootElement> --- With: <xsl:element...
2
by: Julia | last post by:
Hello How can i control the serialize result? I dont need <?xml version="1.0" encoding="utf-8" ?> and "xmlns:xsd=http://www.w3.org/2001/XMLSchema...
0
by: chris.stromberger | last post by:
New to SOAP. I am working on something that uses the paypal web services. All the examples have this as the first portion of the soap doc <?xml version="1.0" encoding="UTF-8"?>...
9
by: MR | last post by:
I get the following Exception "The data at the root level is invalid. Line 1, position 642" whenever I try to deserialize an incoming SOAP message. The incoming message is formed well and its...
6
by: fzhang | last post by:
I am relatively new to XML and C#. So, forgive me if this question is too newbie. :-) While assuming this is an easy programming task, I couldn't find a single reference anywhere for how to do...
3
by: martin1 | last post by:
Hi, All, the xml file is like this: <Root> <AutoData> <Table1> <Data1 10 </Data1> <Data2 20 </Data2> <Data3 40 </Data3>
3
by: Keith Patrick | last post by:
I'm doing some document merging where I want to bring in an XmlDocument and import its document element into another document deeper in its tree. However, when serializing my underlying objects,...
4
by: BorisBoshond | last post by:
Hi all, Hope someone is able and willing to help me with following problem. I received a xsd file from another company, our company i supposed to return xml based on that xsd. Problem is that I...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.