本文介绍了使用Linq或任何其他方式在c#中读取xml文件。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试读取xml文件。但我的文件包含像'&'这样的特殊字符,由于这个符号,我遇到了问题,无法读取xml文件。

i我正在使用以下代码: -



xml文件: -



<?xml version ='1.0'coding ='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>

< To talPages> 1< / TotalPages>

<状态> 4< /状态>

< JobName> Microsoft Wor& d - 复制< / 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< / Bla ckDots>

< 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>









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>




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)
           { }





加载函数给出错误。一旦我删除了特殊字符的工作,但如何用特殊字符读取,因为我无法删除XML中的任何字符。



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 .

推荐答案

<JobName>Microsoft Wor&d - Copy</JobName>



应该是:


Should be:

<JobName>Microsoft Wor&amp;d - Copy</JobName>





在最后一种情况下,所有解析器都会正确解析它。



-SA


这篇关于使用Linq或任何其他方式在c#中读取xml文件。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-22 21:28