当试图解析XML文件,为什么我没有得到任何结果?

当试图解析XML文件,为什么我没有得到任何结果?

问题描述:

using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data; 
using System.Drawing; 
using System.IO; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 
using System.Windows.Forms; 
using System.Xml; 
using System.Xml.Linq; 

namespace XmlParser 
{ 
    public partial class Form1 : Form 
    { 
     public Form1() 
     { 
      InitializeComponent(); 

      ParseAndDisplayXml(@"C:\Users\myxml\Documents\myxml.xml"); 
     } 

     private void ParseAndDisplayXml(string filename) 
     { 
      XDocument document = XDocument.Load(filename); 
      var list = document.Root.Elements("rect") 
       .Select(
       e => new 
       { 
        Width = e.Attribute("width").ToString(), 
        Height = e.Attribute("height").ToString(), 
        X = e.Attribute("x").ToString() 
       } 
       ); 
      string result = ""; 
      foreach (var item in list) 
      { 
       result += string.Format("Width--{0},Height--{1},X--{2}", item.Width, item.Height, item.X); 


      } 
     } 

     private void Form1_Load(object sender, EventArgs e) 
     { 

     } 
    } 
} 

我试图解析的xml的格式部分在xml文件中间: 矩形部分和路径。当试图解析XML文件,为什么我没有得到任何结果?

<?xml version="1.0" encoding="UTF-8" standalone="no"?> 
<!-- Created with Inkscape (http://www.inkscape.org/) --> 

<svg 
    xmlns:dc="http://purl.org/dc/elements/1.1/" 
    xmlns:cc="http://creativecommons.org/ns#" 
    xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" 
    xmlns:svg="http://www.w3.org/2000/svg" 
    xmlns="http://www.w3.org/2000/svg" 
    xmlns:sodipodi="http://sodipodi.sourceforge.net/DTD/sodipodi-0.dtd" 
    xmlns:inkscape="http://www.inkscape.org/namespaces/inkscape" 
    width="210mm" 
    height="297mm" 
    viewBox="0 0 744.09448819 1052.3622047" 
    id="svg2" 
    version="1.1" 
    inkscape:version="0.91 r13725" 
    sodipodi:docname="drawing.svg" 
    inkscape:export-filename="C:\Users\adili_000\Desktop\drawing.png" 
    inkscape:export-xdpi="125" 
    inkscape:export-ydpi="125"> 
    <defs 
    id="defs4" /> 
    <sodipodi:namedview 
    id="base" 
    pagecolor="#ffffff" 
    bordercolor="#666666" 
    borderopacity="1.0" 
    inkscape:pageopacity="0.0" 
    inkscape:pageshadow="2" 
    inkscape:zoom="1.4" 
    inkscape:cx="470.51389" 
    inkscape:cy="692.09768" 
    inkscape:document-units="px" 
    inkscape:current-layer="layer1" 
    showgrid="false" 
    inkscape:window-width="1920" 
    inkscape:window-height="1017" 
    inkscape:window-x="-8" 
    inkscape:window-y="-8" 
    inkscape:window-maximized="1"> 
    <inkscape:grid 
     type="xygrid" 
     id="grid4172" /> 
    </sodipodi:namedview> 
    <metadata 
    id="metadata7"> 
    <rdf:RDF> 
     <cc:Work 
     rdf:about=""> 
     <dc:format>image/svg+xml</dc:format> 
     <dc:type 
      rdf:resource="http://purl.org/dc/dcmitype/StillImage" /> 
     </cc:Work> 
    </rdf:RDF> 
    </metadata> 
    <g 
    inkscape:label="Layer 1" 
    inkscape:groupmode="layer" 
    id="layer1"> 
    <rect 
     style="opacity:1;fill:#00c8fc;fill-opacity:0.98823529;fill-rule:nonzero;stroke:none;stroke-width:1;stroke-linecap:round;stroke-linejoin:miter;stroke-miterlimit:4;stroke-dasharray:none;stroke-dashoffset:0;stroke-opacity:1" 
     id="rect4155" 
     width="45.714287" 
     height="30" 
     x="37.387959" 
     y="115.30345" /> 
    <rect 
     style="opacity:1;fill:#00c8fc;fill-opacity:0.98823529;fill-rule:nonzero;stroke:none;stroke-width:1;stroke-linecap:round;stroke-linejoin:miter;stroke-miterlimit:4;stroke-dasharray:none;stroke-dashoffset:0;stroke-opacity:1" 
     id="rect4155-5" 
     width="45.714287" 
     height="30" 
     x="91.899246" 
     y="115.40621" /> 
    <rect 
<path 
     sodipodi:type="star" 
     style="opacity:1;fill:#f1c8fc;fill-opacity:0.98823529;fill-rule:nonzero;stroke:none;stroke-width:1;stroke-linecap:round;stroke-linejoin:miter;stroke-miterlimit:4;stroke-dasharray:none;stroke-dashoffset:0;stroke-opacity:1" 
     id="path4841" 
     sodipodi:sides="8" 
     sodipodi:cx="288.21429" 
     sodipodi:cy="396.29076" 
     sodipodi:r1="21.58555" 
     sodipodi:r2="10.792775" 
     sodipodi:arg1="1.0471976" 
     sodipodi:arg2="1.4398967" 
     inkscape:flatsided="false" 
     inkscape:rounded="0" 
     inkscape:randomized="0" 
     d="m 299.00707,414.98441 -9.38404,-7.9932 -6.99549,10.1496 -0.98347,-12.28755 -12.12341,2.23029 7.99319,-9.38404 -10.1496,-6.99549 12.28756,-0.98347 -2.23029,-12.12341 9.38404,7.99319 6.99549,-10.1496 0.98347,12.28756 12.12341,-2.23029 -7.99319,9.38403 10.14959,6.99549 -12.28755,0.98348 z" /> 
    </g> 
</svg> 

我想要得到的宽度,高度,X,Y值 但是,当它越来越到的foreach目前还没有结果。 我想从xml文件内容中提取一些数据。

+1

只是猜测没有看到文件的顶部,但也许这是SVG,你需要使用正确的命名空间? – smarx

+0

你应该在一个问题中粘贴完整的xml文档,在这个问题中,你不能解析一个文件,而只能解析它的一部分。 – Evk

+0

@smarx它是svg文件。该文件是svg格式,我在我的文章中发布了另一个名称。 Te文件是drawing.svg,但我认为它是xml。我找不到如何解析svg文件。 –

根据我的评论,您需要使用正确的命名空间来查找要查找的元素,并且由于它们不是根节点的直接子节点,因此您需要使用Descendants而不是Elements。我还将.ToString()的属性更改为.Value,因为我认为这真的是你想要的。

工作代码:

using System; 
using System.Linq; 
using System.Xml; 
using System.Xml.Linq; 

namespace XmlParser 
{ 
    public class Test { 
     public static void Main(string []args) { 
      XDocument document = XDocument.Load("test.svg"); 
      XNamespace ns = "http://www.w3.org/2000/svg"; 

      var list = document.Root.Descendants(ns + "rect").Select(e => new { 
       Width = e.Attribute("width").Value, 
       Height = e.Attribute("height").Value, 
       X = e.Attribute("x").Value 
      }); 

      foreach (var item in list) { 
       Console.WriteLine("Width: {0}, Height: {1}, X: {2}", item.Width, item.Height, item.X); 
      } 

      // Output: 
      // Width: 45.714287, Height: 30, X: 37.387959 
      // Width: 45.714287, Height: 30, X: 91.899246 
     } 
    } 
} 

使用下列固定SVG(删除似乎是一个多余的"<rect"):

<?xml version="1.0" encoding="UTF-8" standalone="no"?> 
<!-- Created with Inkscape (http://www.inkscape.org/) --> 

<svg 
    xmlns:dc="http://purl.org/dc/elements/1.1/" 
    xmlns:cc="http://creativecommons.org/ns#" 
    xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" 
    xmlns:svg="http://www.w3.org/2000/svg" 
    xmlns="http://www.w3.org/2000/svg" 
    xmlns:sodipodi="http://sodipodi.sourceforge.net/DTD/sodipodi-0.dtd" 
    xmlns:inkscape="http://www.inkscape.org/namespaces/inkscape" 
    width="210mm" 
    height="297mm" 
    viewBox="0 0 744.09448819 1052.3622047" 
    id="svg2" 
    version="1.1" 
    inkscape:version="0.91 r13725" 
    sodipodi:docname="drawing.svg" 
    inkscape:export-filename="C:\Users\adili_000\Desktop\drawing.png" 
    inkscape:export-xdpi="125" 
    inkscape:export-ydpi="125"> 
    <defs 
    id="defs4" /> 
    <sodipodi:namedview 
    id="base" 
    pagecolor="#ffffff" 
    bordercolor="#666666" 
    borderopacity="1.0" 
    inkscape:pageopacity="0.0" 
    inkscape:pageshadow="2" 
    inkscape:zoom="1.4" 
    inkscape:cx="470.51389" 
    inkscape:cy="692.09768" 
    inkscape:document-units="px" 
    inkscape:current-layer="layer1" 
    showgrid="false" 
    inkscape:window-width="1920" 
    inkscape:window-height="1017" 
    inkscape:window-x="-8" 
    inkscape:window-y="-8" 
    inkscape:window-maximized="1"> 
    <inkscape:grid 
     type="xygrid" 
     id="grid4172" /> 
    </sodipodi:namedview> 
    <metadata 
    id="metadata7"> 
    <rdf:RDF> 
     <cc:Work 
     rdf:about=""> 
     <dc:format>image/svg+xml</dc:format> 
     <dc:type 
      rdf:resource="http://purl.org/dc/dcmitype/StillImage" /> 
     </cc:Work> 
    </rdf:RDF> 
    </metadata> 
    <g 
    inkscape:label="Layer 1" 
    inkscape:groupmode="layer" 
    id="layer1"> 
    <rect 
     style="opacity:1;fill:#00c8fc;fill-opacity:0.98823529;fill-rule:nonzero;stroke:none;stroke-width:1;stroke-linecap:round;stroke-linejoin:miter;stroke-miterlimit:4;stroke-dasharray:none;stroke-dashoffset:0;stroke-opacity:1" 
     id="rect4155" 
     width="45.714287" 
     height="30" 
     x="37.387959" 
     y="115.30345" /> 
    <rect 
     style="opacity:1;fill:#00c8fc;fill-opacity:0.98823529;fill-rule:nonzero;stroke:none;stroke-width:1;stroke-linecap:round;stroke-linejoin:miter;stroke-miterlimit:4;stroke-dasharray:none;stroke-dashoffset:0;stroke-opacity:1" 
     id="rect4155-5" 
     width="45.714287" 
     height="30" 
     x="91.899246" 
     y="115.40621" /> 
    <path 
     sodipodi:type="star" 
     style="opacity:1;fill:#f1c8fc;fill-opacity:0.98823529;fill-rule:nonzero;stroke:none;stroke-width:1;stroke-linecap:round;stroke-linejoin:miter;stroke-miterlimit:4;stroke-dasharray:none;stroke-dashoffset:0;stroke-opacity:1" 
     id="path4841" 
     sodipodi:sides="8" 
     sodipodi:cx="288.21429" 
     sodipodi:cy="396.29076" 
     sodipodi:r1="21.58555" 
     sodipodi:r2="10.792775" 
     sodipodi:arg1="1.0471976" 
     sodipodi:arg2="1.4398967" 
     inkscape:flatsided="false" 
     inkscape:rounded="0" 
     inkscape:randomized="0" 
     d="m 299.00707,414.98441 -9.38404,-7.9932 -6.99549,10.1496 -0.98347,-12.28755 -12.12341,2.23029 7.99319,-9.38404 -10.1496,-6.99549 12.28756,-0.98347 -2.23029,-12.12341 9.38404,7.99319 6.99549,-10.1496 0.98347,12.28756 12.12341,-2.23029 -7.99319,9.38403 10.14959,6.99549 -12.28755,0.98348 z" /> 
    </g> 
</svg> 
+0

工作。我需要两个更多的参数才能从xml中获取。在每一个矩形有一个填充样式,我需要获得的颜色:style =“opacity:1; fill:#00c8fc所以我需要#00c8fc和一些rects有一个transform:transform =”matrix(0.9672488 ,-0.25383017,0.25383017,0.9672488,0,0)“,我需要得到6个数字:0.9672488,-0.25383017,0.25383017,0.9672488,0,0 –

+0

然后我想把这些参数分配给enum。public enum Rects我不知道如何将每个矩形的值赋给枚举,所以最后我可以从另一个类/脚本访问枚举,并且我将能够键入例如:Rect1.FILL或Rect1.X ....然后Rect2.FILL或Rect2.WIDTH –

+1

@DanielLip听起来你有一些新的问题。如果你问这些新的问题,堆栈溢出效果更好问题而不是在这里评论。 – smarx