在尝试解析xml文件时,为什么我根本没有得到任何结果?

时间:2017-06-02 20:14:33

标签: c# 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 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 个答案:

答案 0 :(得分:2)

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

工作代码:

.Value

使用以下修复的SVG(删除了看似无关的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 } } } ):

"<rect"