【问题标题】:read xml in asp.net c#在asp.net c#中读取xml
【发布时间】:2018-03-07 03:17:24
【问题描述】:

我在访问此 xml 文档中的最后一个节点时遇到问题,该文档遵循 ​​

的格式
   <?mso-application progid="Excel.Sheet"?>
<Workbook xmlns="urn:schemas-microsoft-com:office:spreadsheet" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:x="urn:schemas-microsoft-com:office:excel" xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet" xmlns:html="http://www.w3.org/TR/REC-html40">
<DocumentProperties xmlns="urn:schemas-microsoft-com:office:office">
<Created>2016-11-29T14:40:38Z</Created>
<Version>14.00</Version>
</DocumentProperties>
<OfficeDocumentSettings xmlns="urn:schemas-microsoft-com:office:office">
<RemovePersonalInformation/>
</OfficeDocumentSettings>
<ExcelWorkbook xmlns="urn:schemas-microsoft-com:office:excel">
<WindowHeight>8748</WindowHeight>
<WindowWidth>19428</WindowWidth>
<WindowTopX>0</WindowTopX>
<WindowTopY>108</WindowTopY>
<ProtectStructure>False</ProtectStructure>
<ProtectWindows>False</ProtectWindows>
</ExcelWorkbook>
<Styles>
<Style ss:ID="Default" ss:Name="Normal">
<Alignment ss:Vertical="Bottom"/>
<Borders/>
<Font ss:FontName="Calibri" x:Family="Swiss" ss:Size="11" ss:Color="#000000"/>
<Interior/>
<NumberFormat/>
<Protection/>
</Style>
<Style ss:ID="s62">
<NumberFormat ss:Format="Short Date"/>
</Style>
<Style ss:ID="s63">
<NumberFormat ss:Format="Fixed"/>
</Style>
<Style ss:ID="s64">
<NumberFormat ss:Format="0"/>
</Style>
</Styles>
<Worksheet ss:Name="xml File">
<Table ss:ExpandedColumnCount="7" ss:ExpandedRowCount="771" x:FullColumns="1" x:FullRows="1" ss:DefaultRowHeight="14.55">
<Column ss:Width="57"/>
<Column ss:Width="56.4" ss:Span="1"/>
<Column ss:Index="4" ss:Width="50.4" ss:Span="3"/>
<Row ss:AutoFitHeight="0">
<Cell>
<Data ss:Type="String">Date</Data>
</Cell>
<Cell>
<Data ss:Type="String">ULSP</Data>
</Cell>
<Cell>
<Data ss:Type="String">ULSD</Data>
</Cell>
<Cell>
<Data ss:Type="String">ULSP</Data>
</Cell>
<Cell>
<Data ss:Type="String">ULSD</Data>
</Cell>
<Cell>
<Data ss:Type="String">ULSP</Data>
</Cell>
<Cell>
<Data ss:Type="String">ULSD</Data>
</Cell>
</Row>

我已尝试使用以下内容,但遇到了问题。读取 lastnode 元素只会返回几乎所有的 xml 文档

字符串 id = "2003-06-16T00:00:00.000"; //要选择的id

    XElement Contact = (from xml2 in doc.Descendants("cell")
                        where xml2.Element("data").Value == id
                        select xml2).FirstOrDefault();

【问题讨论】:

  • 单元格和“id”的数据类型是什么?他们可能不匹配。使用 Value 属性不是一个好主意。最好使用以下内容: (int)xml2.Element("data")
  • 您还需要一个命名空间,因此请尝试以下操作:XElement root = doc.Root; XNamespace ns = root.GetDefaultNamespace(); XElement Contact = (from xml2 in doc.Descendants(ns + "cell") where (int)xml2.Element(ns + "data") == id select xml2).FirstOrDefault();

标签: c# asp.net xml


【解决方案1】:

虽然想法不好,但拆分在这里会像魅力一样发挥作用......

//data is your xml data in plain text format without modification
String sp1 = data.Split(new string[] { "<Created>" }, StringSplitOptions.None)[1];
String sp2 = sp1.Split(new string[] { "</Created>" }, StringSplitOptions.None)[0];
//sp2 is the required string you want which has value 2003-06-16T00:00:00.000

希望这会有所帮助!干杯!!

【讨论】:

  • 您好先生,感谢您帮助回答我的问题,XML 文件是燃料数据列表,如果您不介意花时间,可以在以下链接中查看,我希望的数据extract 是最新的燃料数据,因为 xml 包含自 2003 年以来的所有燃料数据。感谢您抽出宝贵时间
  • lel bro 不要叫我先生 :v
  • 在我解决了你的问题后,请对我说声谢谢所以你的意思是有多个创建的标签?
  • 你能把整个xml文件的链接发给我吗?
猜你喜欢
  • 2011-04-24
  • 1970-01-01
  • 2016-01-03
  • 2017-02-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多