Click here to Skip to main content
16,012,468 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I am trying to read an xml file . But my files conatins special character like '&' , due to this symbol i am getting problem and unable to read xml file .
i am using following code :-

xml file :-

<?xml version='1.0' encoding='UTF-8' ?>
<Job>
<PrinterName>NPI4A37C7</PrinterName>
<PrinterIpAddress>192.168.35.100</PrinterIpAddress>
<PrinterModel number="Q7830A">HP LaserJet M5035 MFP</PrinterModel>
<PrinterSerialNumber>CN5XBDX803</PrinterSerialNumber>
<PrinterFormatterNumber>GXG014A</PrinterFormatterNumber>
<PrinterColorSupport>1</PrinterColorSupport>
<PrinterDuplexSupport>0</PrinterDuplexSupport>
<MibId>43</MibId>
<JobId>13092736-4135-7362-3092-130927362413</JobId>
<Size>30511</Size>
<TotalPages>1</TotalPages>
<Status>4</Status>
<JobName>Microsoft Wor&d - Copy</JobName>
<MachineName>PCA0040</MachineName>
<DomainName>sinarmasproperty.com</DomainName>
<UserName>MAYAWEHANTOUW</UserName>
<SubmittedDate>2011-06-28 15:07:56</SubmittedDate>
<PrintedId>13092736-4137-7362-3092-130927362413</PrintedId>
<Simplex>1</Simplex>
<Duplex>0</Duplex>
<GrayScale>1</GrayScale>
<Color>0</Color>
<Orientation>0</Orientation>
<Economode></Economode>
<MediaWidth>210.0</MediaWidth>
<MediaHeight>297.0</MediaHeight>
<MediaSize>a4</MediaSize>
<MediaType>2</MediaType><BlackDots>521</BlackDots>
<YellowDots>0</YellowDots>
<CyanDots>0</CyanDots>
<MagentaDots>0</MagentaDots>
<Finishing>0</Finishing>
<Language></Language>
<JobType>1</JobType>
<GeneratedBy>SecureJet-Track-EM</GeneratedBy><Sheet>1</Sheet>
</Job>




C#
try
           {
               // XElement main = XElement.Load(file);
               XDocument doc = XDocument.Load(filepath);

               var query = from d in doc.Root.Descendants("Jobs") select d;

               foreach (var q in query)
               {
                   string name = q.Element("PrinterName").Value;
                   string ssn = q.Element("PrinterIpAddress").Value;
                   string address = q.Element("PrinterSerialNumber").Value;
               }
           }
           catch(Exception ex)
           { }


Load function is giving the error . once i removed special character its working but how to read with special character as i can not remove any character in XML .
Posted
Updated 7-Jul-13 20:43pm
v4

1 solution

It's not nice to ask such question without giving any information on the "problem" and not showing offending XML.

However, chances are, your XML is not well formed. Indeed '&' is "special", but it never creates any problems. It is either used in the markup (character entities), or it is a part of data and should be escaped via a character HTML entity of CDATA block:
http://www.w3.org/TR/html4/sgml/entities.html[^],
http://en.wikipedia.org/wiki/CDATA[^].

If you have a well-formed XML, it will be successfully parser with any of the parsers available in .NET BCL.

[EDIT]

After OP's clarification.

Yes, this is what I thought.

This line makes XML not well-formed:
HTML
<JobName>Microsoft Wor&d - Copy</JobName>

Should be:
HTML
<JobName>Microsoft Wor&amp;d - Copy</JobName>


In the last case, all parsers will parse it correctly.

—SA
 
Share this answer
 
v2
Comments
lalit.mca2006 8-Jul-13 2:07am    
i have attached xml file with bold offended lines.
Sergey Alexandrovich Kryukov 8-Jul-13 10:49am    
Thank you. This is what I thought.
—SA
lalit.mca2006 9-Jul-13 4:59am    
Thank you so much sergey for this solution . But i would like to ask one more thing that I can not change XML whatever is existing xml i will have to read . As its predefined i can not change in XML . Then what should be the method to read this kind of XML like <jobname>Microsoft Wor&d - Copy.
Thanks in advANCE .
Sergey Alexandrovich Kryukov 9-Jul-13 10:20am    
Why would you want to read XML if it is not XML. It is NOT XML, face it. You should reject it.
—SA
lalit.mca2006 9-Jul-13 5:05am    
is there any way to read manually rather than using xml parser ?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900