XML Serialization without Attributes?

Hello,

I'm programming with Visual Studio 2005 for .net 2.0 in C#. Very often I
use XML Serilization [1].
Some of the output files can be edited by the user with an external
editor. What I don't like are some attributes which are inserted into
the fily by the serializer, and which may confuse the user.

Example:
| <?xml version="1.0"?>
| <Summary xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd = "http://www.w3.org/2001/XMLSchema">
|       <CurrentCQI>
|             <Incident>
|                   <CQI>
<snip>

I'd like to have something like that:
| <Summary>
|       <CurrentCQI>
|             <Incident>
|                   <CQI>
<snip>

The "?xml version" and xmlns attributes are not needed for
deserialization. XML files which were written manually without this
information are deserialized without problem.

Is there a possibility to tell the serializer to leave this data out of
the output file?

TIA,
Christian

[1] Here's the kind of code I use for serialization:

public object Data; // The object to be saved, set by a different method
private Type SType; // The type of the object
private FileStream DStream;    
private XmlSerializer XmlSer;  

SType = Data.GetType();         
XmlSer = new XmlSerializer(SType);    

DStream = new FileStream(p, FileMode.CreateNew);    
XmlSer.Serialize(DStream, Data);                    
0
11/7/2009 3:03:24 PM
dotnet.xml 7266 articles. 0 followers. Follow

3 Replies
1335 Views

Similar Articles

[PageSpeed] 48

Christian Treffler wrote:

> I'm programming with Visual Studio 2005 for .net 2.0 in C#. Very often I
> use XML Serilization [1].
> Some of the output files can be edited by the user with an external
> editor. What I don't like are some attributes which are inserted into
> the fily by the serializer, and which may confuse the user.
> 
> Example:
> | <?xml version="1.0"?>
> | <Summary xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd = "http://www.w3.org/2001/XMLSchema">
> |       <CurrentCQI>
> |             <Incident>
> |                   <CQI>
> <snip>
> 
> I'd like to have something like that:
> | <Summary>
> |       <CurrentCQI>
> |             <Incident>
> |                   <CQI>
> <snip>
> 
> The "?xml version" and xmlns attributes are not needed for
> deserialization. 

<?xml version="1.0"?> is not an attribute, it is the XML declaration. If 
you don't want that then serialize to an XmlWriter with the 
XmlWriterSettings specifying to omit the XML declaration e.g.

             XmlWriterSettings xws = new XmlWriterSettings();
             xws.OmitXmlDeclaration = true;
             xws.Indent = true;

             XmlSerializer ser = new XmlSerializer(typeof(Foo));

             using (XmlWriter writer = XmlWriter.Create(@"file.xml", xws))
             {
                 ser.Serialize(writer, fooInstance);
                 writer.Close();
             }
-- 

	Martin Honnen --- MVP XML
	http://msmvps.com/blogs/martin_honnen/
0
mahotrash (1778)
11/7/2009 4:46:55 PM
Christian Treffler wrote:

> I'm programming with Visual Studio 2005 for .net 2.0 in C#. Very often I
> use XML Serilization [1].
> Some of the output files can be edited by the user with an external
> editor. What I don't like are some attributes which are inserted into
> the fily by the serializer, and which may confuse the user.
> 
> Example:
> | <?xml version="1.0"?>
> | <Summary xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd = "http://www.w3.org/2001/XMLSchema">
> |       <CurrentCQI>
> |             <Incident>
> |                   <CQI>
> <snip>
> 
> I'd like to have something like that:
> | <Summary>
> |       <CurrentCQI>
> |             <Incident>
> |                   <CQI>
> <snip>
> 
> The "?xml version" and xmlns attributes are not needed for
> deserialization. XML files which were written manually without this
> information are deserialized without problem.

If you think you don't need them then you can get rid of the xmlns:xsi 
and xmlns:xsd attributes as follows:

             XmlSerializer ser = new XmlSerializer(typeof(Foo));
             XmlSerializerNamespaces ns = new XmlSerializerNamespaces();
             ns.Add("", "");

             using (XmlWriter writer = XmlWriter.Create(@"file.xml", xws))
             {
                 ser.Serialize(writer, fooInstanceObject, ns);
                 writer.Close();
             }


-- 

	Martin Honnen --- MVP XML
	http://msmvps.com/blogs/martin_honnen/
0
mahotrash (1778)
11/7/2009 6:48:51 PM
Martin Honnen wrote:

> <?xml version="1.0"?> is not an attribute, it is the XML declaration. If 
> you don't want that then serialize to an XmlWriter with the 
> XmlWriterSettings
<snip>

Martin, thank you for your inputs. I implemented them and found that the
XmlWriterSettings provided some additional means to control the output. 
That was very useful.

Christian
0
11/9/2009 2:30:48 PM
Reply:

Similar Artilces:

XML Header
Hi, I have some XML doc loaded in a C# Web Service. XmlDocument doc = new XmlDocument(); doc.LoadXml(myDoc); Code Missing to manipulate header (I would like to replace or manupulate all these XXXXX): <mso-XXXXXX productversion="XXXXXX"> <mso-application progid="XXXXX"?> doc.Save(myNewDoc); Any idea on how to access and manipulate data within the doc header? Thanks vbdev vb6dev2003 wrote: > I have some XML doc loaded in a C# Web Service. > XmlDocument doc = new XmlDocument(); > doc.LoadXml(myDoc); > > Code Missing to manipulate header ...

broadcusting a message without exposing the recepients addresses
If I have a number of customers to whom I need to regularly send a message (same contents for all customers), but I do not want addresses of other customers to appear on a message received by a particular customer, how do I automate this in Outlook? You could make a group of customers, then use that to select for the BCC field. On Tue, 26 Oct 2004 12:42:52 +0100, "aa" <aa@virgin.net> wrote: >If I have a number of customers to whom I need to regularly send a message >(same contents for all customers), but I do not want addresses of other >customers to appear on a ...

Radiant 1510 with samsung 350 serial printers
good day, Has anyone ever tried using Radiant 1510 Touchscreen moniters with samsung 350 serial printers......I am having some trouble being able to print....i have triend both the opos and windows drivers....any help would be greatly appreciated.... Best regards, Uttam Using a serial printer might be your problem. If you look through past threads you will notice a lot of the problems listed with printers show up with a serial connection. It seems to be much better to use either LPT1 or USB. You might want to consider a Serial to USB adapter and see if that works. Craig "...

Read XHTML into XML
Hi all, I need to read/parse XHTML aspx pages and look for certain tokens and content. How can I use a XmlTextReader for this? If not, any other ideas? Thanks in advance, JA Reyes. Jose Antonio Reyes wrote: > I need to read/parse XHTML aspx pages and look for certain tokens and > content. How can I use a XmlTextReader for this? If not, any other ideas? If the pages are well-formed XHTML then it is possible to use XmlReader (in .NET 2.0/3.0) or XmlTextReader (in .NET 1.x) to parse the XHTML documents. You can also use the other XML APIs .NET provides so using XPathNavigator and/o...

How to invoke the xml editor?
I installed vs2005 on a new xp pro machine, and installed everything except C++. When I click on a file with a ".xml" extension it is opened with IE. When I click file/Edit with XML Editor, it opens it with Notepad. If I click File/Open With and select XML Editor, it opens it with IE. So, where is the xml editor? Can it be invoked from withing VS? Where? Bill * billsahiker@yahoo.com wrote in microsoft.public.dotnet.xml: >I installed vs2005 on a new xp pro machine, and installed everything >except C++. When I click on a file with a ".xml" extension it is >opened ...

Serial port
Hey, I've got an application (VC6), which must communicate with the serial port (com port). I use ReadFile, WriteFile with Non-overlapped mode. There's a thread, which is only for the reading. The problem: Missing data. Does anybody knows, how the input/output buffer of the comport is realizied? When there come more data over the ComPort then the Software reads out, what happens? Does anybody had some advices? Thanks Peter There are a variety of ways to set up reading a commport, but in general you= have to use overlapped IO. The comm port driver in effect puts data in y...

CEPC, serial KITL
Can somebody explain to the novice how to enable serial KITL in WinCE 6.0. CEPC ? My CEPC platform has an incompatible Ethernet adapter, so I am booting to MS-DOS copying NK.BIN from the network share and then staring it with loadcepc.exe It works great, I also can see debug messages on serial port. Great, however, what is the easiest way to run KITL over the serial port ? There suppose to be serial KITL implementation in CEPC BSP, but how to enable it ??? Thank you, Yan ...

show XML schema and values
I have an XML document that looks like the following: <record> <Field1>value1</Field1> <Field2>value2</Field2> <Field3>value3</Field3> </record> What I'm aiming to do is to show the data in a report as in the following example: record: Field1 value1 Field2 value2 Field3 value3 The "Field1-3"-elements can have any name, so I'm trying to get a table that has in one result column the element name, and in the second colum...

Making attribute which is a picklist a primary attribute/key
I have an entity with one attribute and is a picklist, I want it to be a primary attribute but it does not give me that option, it shows only nvarchar from the drop down list -- donn Yes you are right, primary field can only be nvarchar. You may change the schema name of primary attribute at first place but cannt change it's type. -- PLEASE do click on Yes or No button if this post was helpful or not for our feedback. uMar Khan Email for direct contact: imumar at gmail dot com "Donald" wrote: > I have an entity with one attribute and is a picklist, I want it to be a...

data lable don't appear in chart bars appear without lable
I have created six panel charts work fine but charts in panel don't lables for bars, value do appears. ...

Printing without certain text (i.e., answers, notes)
Does anyone know an easy way to print two versions of a document? For example, I need to print a student version without answers and instructor's notes. Thanks, TM One way would be to use a style (call it Answers for example), when you want to print out the students copies change the color of the style Answers to white. That should do it. "TM" <t_mccollough@yahoo.com> wrote in message news:e6czHS2YDHA.1384@TK2MSFTNGP10.phx.gbl... > Does anyone know an easy way to print two versions of a document? For > example, I need to print a student version without answers an...

Offline synch using XML web-services
This is a bit long-winded. Can somebody read thru these 2 use cases, and tell me if it is feasible ? :- Use Case : Replication before Offline Synch Scenario 1 : Oracle Server , MSDE client 1. User clicks on 'Go Offline'. System creates a MSDE SQL database in laptop (client), gets the schemas of the tables in Oracle, creates corresponding tables in client's MSDE SQL (mapping Oracle datatypes to SQL datatypes). 2. Offline replication program reads in the tables of the server into a typed datasets, converts the typed datasets into XML, and stores the XML in the client. (XML serial...

problem when transferring out serialized item
we have a serialized item that was returned by a customer, and was placed into offline inventory. i went to offline inventory and transferred the item out, we did a standard transfer type (we are sending the item back to the supplier). after doing that, i went to the transfer out order, and selected issue all, and tried to commit to it. i get the error message "the following serial number exists in the database, and is not available for issuing: 04215494019" i checked this serial number in the serial table, and the status of the serial number is set at 3. we've never...

CRichEditView without OLE : possible ?
Hello, Is there a way to use a CRichEditDoc/CRichEditView without OLE ? I'm developping an application which only use rich text (neither external object nor image). Also, do you confirm me that the part wich are about OLE are those ? - AfxOleInit fct - CntrItem.h|cpp files - "edition/special paste" menu item - the menus items about object insert and links - the drag n' drop feature Let me know ...

XML Serialization ... how to truncate <?xml...
While xml serializing object how can I truncate <?xml ...?> this line alone? I need to send the serialized xml data to a web method ... since SOAP already have <?xml tag there was some confusion Any help would be greatly appreciated Thank you Regards Raj Raj wrote: > While xml serializing object how can I truncate <?xml ...?> this line alone? Serialize to an XmlWriter created with XmlWriterSettings where you set OmitXmlDeclaration to true. -- Martin Honnen --- MVP XML http://msmvps.com/blogs/martin_honnen/ ...

String to xml document
Hi there, I was hoping someone can help me with a little problem I can't seem to find any answers to. I've got a form wich will be posted. On receiving the post I would like to parse a formvar to a xml document. The contents of this formvar (string) will be a valid xhtml document. Can someone give me a startertip (or more :-)) on how to achieve this? tnx in advance, James van der Veen Sjeems wrote: > I was hoping someone can help me with a little problem I can't seem to find > any answers to. > > I've got a form wich will be posted. On receiving the p...

Beginner of XML
I am a beginner of XML. I want to group some files together with a xml file. (it includes a text file, and an image file and a video file) Can someone show me some guidelines? Please help. ...

Valid xml to read from an xml file?
I'm using vb.net, i get an error at the reader line saying the remote server can't be found. Is the below even valid? dim reader as XmlTextReader reader = new XmlTextReader ("http://www.mysite.com/my.xml") do while (reader.Read()) 'do some action here response.write(reader) loop thanx chumley XmlTextReader uses System.Net.WebRequest under the covers -- see if you can figure out how to create a connection that way. Usually there's some proxy server information missing that you can enable if you use WebRequest directly, and then pass the result of GetResponse()...

Example eConnect XML documents
MSDN says the following: Installing eConnect schema When you include the schemas component of the eConnect install, the installer places schema files in a schemas folder on your computer. The following schema resources are available: The install places the .xsd schema files in the directory c:\Program Files\Microsoft Great Plains\eConnect9\XML Schema\Incoming XSD Individual Schemas. The files in the directory contain the schema for each eConnect XML document. The install places a file named eConnect.xsd that contains the schema definition for all eConnect XML documents. The install typicall...

Crazy for the HD movies without Blu-ray player
Now-days Blu-ray Disc is well known for storing high-definition video, PlayStation 3 video games, and another data, with up to 25 GB per single layered, and 50 GB per dual layered disc. It's really a great temptation for all movie lovers. However, you can't enjoy it if you treat it just like an ordinary DVD. Why? Have you ever heard about Digital rights management (DRM) yet? Well blu-ray disc was mainly in three kinds of DRM, including AACS, BD+, ROM Mark. It means that you have to pay another amount of money for a professional blu-ray player to enjoy it. It sounds like that not very ...

How to fill down without changing the format?
Does anyone know how to fill a cell's formula/value down without changing the format (color, font, etc.)? -- _________________________________________ Todd Bennett Mechanical Designer Celerity Group, Inc. 9660 SW Herman Rd Tualatin, OR 97062 Phone: 888-724-3221 E-mail: tbennett@celerity.net Just drag it down. "T Bennett" <tbennett@celerity.net> wrote in message news:big7g7$8skmo$1@ID-183945.news.uni-berlin.de... > Does anyone know how to fill a cell's formula/value down without changing > the format (color, font, etc.)? > > -- > __________________...

Saving DataSet as XML in ADO format using VB.NET
Hi, I am trying to convert a lot of web reports from ASP/VB to ASP.NET/VB.NET. I would like to save my dataset in XML and then use XSLT to transform the XML. The catch is I would like to have the XML in the format that ADO uses versus the default format in which ADO.NET uses. Currently, I am using ADO within .NET, but I am concerned about the performance and it might improve if I used the native data providers for .NET. I am not sure if I am missing something but how do I save the .NET DataSet to XML in the OLD ADO format? Thanks for you help in advance and it is most appreciated. In the ...

XML variable for description on receipt?
Hi, I would like to add a description (actually all of those 3 extended descriptions) to display on the printed receipt under the main description. What is the variable to do that? Entry.Item.Description Entry.Item.ExtendedDescription Entry.Item.SubDescription1 Entry.Item.SubDescription2 Entry.Item.SubDescription3 R. "Zorian" <Zorian@discussions.microsoft.com> wrote in message news:F4425EAC-62B6-46FE-826A-D8BA2AA75DBB@microsoft.com... > Hi, I would like to add a description (actually all of those 3 extended > descriptions) to display on the printed receipt un...

How do I input email addresses in Excel without the hyperlink,
I need email addresses in my excel worksheet, but they must be text, not hyperlink. Iv'e been copying them to a word doc, then doing the "paste special" to put it in excel as text. That works, but is very time consuming. Is there a way to format a column so emails are text only? -- "You can have everything in life you want, if you will just help enough other people get what they want." -- Zig Ziglar Precede the email address with an apostrophe (') when keying. HTH Jason Atlanta, GA >-----Original Message----- >I need email addresses in my excel wor...

xml to SQL
Hey we have a project in which a company produces a XML page in reply to actions on it's site. I see that SQL 2000 works well with XML so Can someone point me in the right direction, I would like to take the XML from thier SQL and have it "post" to own sql server (add,create, data, modifity and delete data on the fly). I was looking at doing XML to HTML and then passing to some a ASP but if we can grab from the responsetext then we could save time and headaches. Now I really know next to nothing about XML but I am reading all the newsgroups and pulled now TONS of xml / sql stuff ...