使用Linq或任何其他方式在c#中读取xml文件。 [英] Read xml file in c# using Linq or any other way .

查看:72
本文介绍了使用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 .

推荐答案

在不给出的情况下问这样的问题并不好有关问题的任何信息,而不是显示有问题的XML。



但是,很可能,您的XML格式不正确。确实'&'是特殊的,但它永远不会产生任何问题。它或者用在标记(字符实体)中,或者它是数据的一部分,应该通过CDATA块的字符HTML实体进行转义:

http://www.w3.org/TR/html4/sgml/entities.html [ ^ ],

http://en.wikipedia.org/wiki/CDATA [ ^ ]。



如果您有一个格式良好的XML,它将成功解析任何.NET BCL中可用的解析器。







在OP澄清之后。 />


是的,这就是我的想法。



此行使XML格式不正确:

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.



After OP's clarification.

Yes, this is what I thought.

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



应该是:


Should be:

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





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



-SA


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

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆