Solved

xml serialization formatting !!!

Posted on 2004-09-26
8
1,332 Views
Last Modified: 2012-05-05
Experts,

I am trying xml serialization using c# and the final xml i get contains lot of escape and formatting characters, i need a clean xml ,how do i get this ,i tried setting the encoding format with no use. pls reply.

sample xml after serialization given - i want to remove the \, \r,\n. I may say string.replace() after getting the xml , but is therre any way to specify it in the xmlserializer???


<?xml version=\"1.0\"?>\r\n<AirAvailability_6_2>\r\n  <AirAvailMods>\r\n    <AirVPrefInd>\r\n      <AirVIncExcInd>I</AirVIncExcInd>\r\n      <RelaxAirVPref>N</RelaxAirVPref>\r\n    </AirVPrefInd>\r\n    <AirVPrefs>\r\n      <AirVAry>\r\n        <AirVInfo>\r\n          <AirV>EK</AirV>\r\n        </AirVInfo>\r\n      </AirVAry>\r\n    </AirVPrefs>\r\n    <GenAvail>\r\n      <NumSeats>2</NumSeats>\r\n      <Class>Y</Class>\r\n      <StartDt>20041201</StartDt>\r\n      <StartPt>DXB</StartPt>\r\n      <EndPt>LHR</EndPt>\r\n      <TmWndInd>D</TmWndInd>\r\n      <EndTmWnd>2330</EndTmWnd>\r\n      <JrnyTm />\r\n      <FltTypeInd />\r\n      <FltTypePref />\r\n      <StartPtInd />\r\n      <EndPtInd />\r\n    </GenAvail>\r\n    <ConxPrefInd>\r\n      <IncExc>I</IncExc>\r\n    </ConxPrefInd>\r\n    <ConxPref>\r\n      <PtAry>\r\n        <PtInfo>\r\n          <Pt>LHR</Pt>\r\n        </PtInfo>\r\n        <PtInfo>\r\n          <Pt>LCY</Pt>\r\n        </PtInfo>\r\n      </PtAry>\r\n    </ConxPref>\r\n  </AirAvailMods>\r\n</AirAvailability_6_2>


and finally, how do i replace \ in the string <?xml version=\"1.0\"?> with spaces (<?xml version="1.0"?>). I tried string.replace("\\","") doesnt work, any idea?

Thanks
Sree
0
Comment
Question by:msreekm
[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
8 Comments
 
LVL 19

Expert Comment

by:drichards
ID: 12155132
Where are you seeing all these escape sequences?  The C# debugger shows you the escape sequences.  So, for instance, when you see:

    <?xml version=\"1.0\"?>

there are no \ characters in the string.  It is showing you the expanded escape sequence '\"' rather than simply the funal " character as you get with say the C++ debugger.  That's why your replace line does not appear to do anything.  Same for all the \r\n's.  These are CRLF pairs that add whitespace into the XML so it is more readable (element tags start on new lines).  I don't think you really need to worry about these.  Do you actually see the four characters \r\n in your file output?
0
 
LVL 10

Expert Comment

by:123654789987
ID: 12158813
If u can save the data as a .Xml file then it will be in correct format
0
 
LVL 10

Expert Comment

by:123654789987
ID: 12158849
To get a well formed XMl U have to make use of the XmlTextWriter class

http://abstractvb.com/code.asp?A=977
0
 
LVL 4

Author Comment

by:msreekm
ID: 12159525
drichards ... Appreciate ur reply.
Its true that replace function doesnt work with \
but it works with \r\n

and iam able to see them in the watch window not in file.

My problem is iam giving this xml string to a component which is giving error due to the <?xml version=\"1.0\"?>

when i correct it to <?xml version="1.0"?>  it works...


Sree
0
 
LVL 7

Accepted Solution

by:
NipNFriar_Tuck earned 500 total points
ID: 12160454
Hmmm you could try a regex...

string inString = "Whatever";
string out1 = Regex.Replace( inString, @"\r\n" );
string outString = (Regex.Replace( out1, @"\" );

Then check outString... note that if you check this in the debug environmnet you WILL see the " escaped with a \ , so you will need to spit it to the console or a message box.
0

Featured Post

Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

Question has a verified solution.

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

Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
Come and listen to Percona CEO Peter Zaitsev discuss what’s new in Percona open source software, including Percona Server for MySQL (https://www.percona.com/software/mysql-database/percona-server) and MongoDB (https://www.percona.com/software/mongo-…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …

719 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