473,386 Members | 1,757 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,386 software developers and data experts.

How to preserve root node attributes on serialization

It seems that XML Serializatoin automatically puts certain XSD attributs in
the root node. How can I control the root node attributes emitted to
something more custom so I can preserve my schema link?

Nov 11 '05 #1
4 2191
Alex wrote:
It seems that XML Serializatoin automatically puts certain XSD attributs in
the root node. How can I control the root node attributes emitted to
something more custom so I can preserve my schema link?


Just create public fields and reflect them to these attributes using
usual XmlAttribute attribute.
--
Oleg Tkachenko
http://www.tkachenko.com/blog
Multiconn Technologies, Israel

Nov 11 '05 #2
On [GMT+0100=CET],
Oleg Tkachenko <oleg@NO_SPAM_PLEASEtkachenko.com> thought hard and spewed:
Alex wrote:
It seems that XML Serializatoin automatically puts certain XSD
attributs in the root node. How can I control the root node
attributes emitted to something more custom so I can preserve my
schema link?


Just create public fields and reflect them to these attributes using
usual XmlAttribute attribute.


Do I have to do anything special besides place public fields in my root
serialization object and apply XmlAttribute to them? It doesn't seem to
work, I get reflection error, trying to do typeof(SerializtionObj)
Nov 11 '05 #3
Are you just trying to get rid of the prefix definitions for xsd and xsi? In
that case you need to pass an empty XmlSerializerNamespaces object to the
Serialize method as discussed in this thread [0]

--
HTH
Christoph Schittko [MVP]
Software Architect, .NET Mentor

[0] http://www.dotnet247.com/247referenc...26/130196.aspx

"Alex" <a.*******@verizon.net> wrote in message
news:uy*************@TK2MSFTNGP12.phx.gbl...
On [GMT+0100=CET],
Oleg Tkachenko <oleg@NO_SPAM_PLEASEtkachenko.com> thought hard and spewed:
Alex wrote:
It seems that XML Serializatoin automatically puts certain XSD
attributs in the root node. How can I control the root node
attributes emitted to something more custom so I can preserve my
schema link?


Just create public fields and reflect them to these attributes using
usual XmlAttribute attribute.


Do I have to do anything special besides place public fields in my root
serialization object and apply XmlAttribute to them? It doesn't seem to
work, I get reflection error, trying to do typeof(SerializtionObj)

Nov 11 '05 #4
Alex wrote:
Do I have to do anything special besides place public fields in my root
serialization object and apply XmlAttribute to them? It doesn't seem to
work, I get reflection error, trying to do typeof(SerializtionObj)


Here is a sample how to control schemaLocation attribute, I hope it helps:

public class Test {
[XmlAttribute("schemaLocation",
Namespace="http://www.w3.org/2001/XMLSchema-instance")]
public string schemaLocation;

static void Main(string[] args) {
XmlSerializer ser = new XmlSerializer(typeof(Test));
Test t = new Test();
t.schemaLocation = "foo.xsd";
ser.Serialize(Console.Out, t);
}
}

--
Oleg Tkachenko
http://www.tkachenko.com/blog
Multiconn Technologies, Israel

Nov 11 '05 #5

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

Similar topics

6
by: David B. Bitton | last post by:
I am having a problem deserializing XML when the root node is missing a namespace declaration. My Type has an XmlTypeAttribute with a namespace defined. If I attempt to deserialize the XML, I get...
16
by: Bob Rock | last post by:
Hello, when serializing an array of elements of a class Classname using XmlSerializer.Serialize() I get an XML like the following: <?xml version="1.0"> <ArrayOfClassname> ....... ..........
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...
9
by: Mark Olbert | last post by:
I'm trying to serialize (using XmlSerializer.Serialize) a class that I generated from an XSD schema using XSD.EXE /c. The problem I'm running into is that the root element needs to be unqualified,...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.