0
我想根據id
更改元素rect
的style
屬性的值。代碼如下。編譯器說這個節點是空的。通過節點編輯SVG的屬性值會導致錯誤
<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
<!-- Created with Inkscape (http://www.inkscape.org/) -->
<svg xmlns:osb="http://www.openswatchbook.org/uri/2009/osb" 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:xlink="http://www.w3.org/1999/xlink" xmlns:sodipodi="http://sodipodi.sourceforge.net/DTD/sodipodi-0.dtd" xmlns:inkscape="http://www.inkscape.org/namespaces/inkscape" width="10.2332in" height="11.252161in" viewBox="0 0 920.98797 1012.6946" id="svg2" version="1.1" inkscape:version="0.91 r13725" sodipodi:docname="map.svg">
<defs id="defs4">
<linearGradient inkscape:collect="always" id="linearGradient6146">
<stop style="stop-color:#f2f2f2;stop-opacity:1;" offset="0" id="stop6148" />
<stop style="stop-color:#f2f2f2;stop-opacity:0;" offset="1" id="stop6150" />
</linearGradient>
<linearGradient id="linearGradient6140" osb:paint="solid">
<stop style="stop-color:#000000;stop-opacity:1;" offset="0" id="stop6142" />
</linearGradient>
<linearGradient inkscape:collect="always" xlink:href="#linearGradient6146" id="linearGradient6152" x1="757.7793" y1="1019.3268" x2="1032.8184" y2="1019.3268" gradientUnits="userSpaceOnUse" />
<linearGradient inkscape:collect="always" xlink:href="#linearGradient6146" id="linearGradient6154" gradientUnits="userSpaceOnUse" x1="757.7793" y1="1019.3268" x2="1032.8184" y2="1019.3268" />
</defs>
<sodipodi:namedview id="base" pagecolor="#ffffff" bordercolor="#666666" borderopacity="1.0" inkscape:pageopacity="0.0" inkscape:pageshadow="2" inkscape:zoom="0.41986485" inkscape:cx="665.09998" inkscape:cy="547.70001" inkscape:document-units="px" inkscape:current-layer="layer1" showgrid="false" units="in" fit-margin-left="-2" fit-margin-right="0" fit-margin-top="-1" fit-margin-bottom="0" inkscape:window-width="1366" inkscape:window-height="705" inkscape:window-x="-8" inkscape:window-y="-8" inkscape:window-maximized="1" width="7.5in" height="9000in">
<inkscape:grid type="xygrid" id="grid3336" originx="-49.686342" originy="-348.02656" />
</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" />
<dc:title>
</dc:title>
</cc:Work>
</rdf:RDF>
</metadata>
<g inkscape:label="Layer 1" inkscape:groupmode="layer" id="layer1" transform="translate(141.31935,-164.56211)">
<ellipse style="fill:#000000;fill-rule:evenodd;stroke:#000000;stroke-width:1.47842932px;stroke-linecap:butt;stroke-linejoin:miter;stroke-opacity:1" id="path3349" cx="179.14752" cy="213.21657" rx="21.40518" ry="14.970306" />
<ellipse style="fill:#ffffff;fill-opacity:1;fill-rule:evenodd;stroke:#000000;stroke-width:1.47842932px;stroke-linecap:butt;stroke-linejoin:miter;stroke-opacity:1" id="path3349-5" cx="178.278" cy="548.51111" rx="21.40518" ry="14.970306" />
<path style="fill:none;fill-rule:evenodd;stroke:#000000;stroke-width:1.18673158px;stroke-linecap:butt;stroke-linejoin:miter;stroke-opacity:1" d="m 178.10765,231.18477 c -2.42669,297.78248 -2.42669,297.78248 -2.42669,297.78248 l 0,-1.11949 2.42669,1.11949 0,0" id="path4175" inkscape:connector-curvature="0" />
<rect style="fill:#ffffff;fill-opacity:1" id="rect4182" width="157.72238" height="44.910931" x="-20.258638" y="180.71774" />
<rect style="fill:#ffffff;fill-opacity:1" id="rect4184" width="76.607979" height="37.4258" x="175.63919" y="74.562111" />
<rect style="fill:#ffffff;fill-opacity:1" id="rect4186" width="135.19063" height="52.396061" x="-90.107155" y="203.17317" />
<path style="fill:#000000;fill-rule:evenodd;stroke:#000000;stroke-width:1.05582392px;stroke-linecap:butt;stroke-linejoin:miter;stroke-opacity:1" d="m 148.7296,547.49026 -220.637008,0 0,0" id="path4288" inkscape:connector-curvature="0" />
<g style="font-style:normal;font-weight:normal;font-size:40px;line-height:125%;font-family:sans-serif;letter-spacing:0px;word-spacing:0px;fill:#000000;fill-opacity:1;stroke:none;stroke-width:1px;stroke-linecap:butt;stroke-linejoin:miter;stroke-opacity:1" id="flowRoot4302" />
<ellipse style="fill:none;fill-rule:evenodd;stroke:#000000;stroke-width:1.47842932px;stroke-linecap:butt;stroke-linejoin:miter;stroke-opacity:1" id="path3349-1-0" cx="-87.03698" cy="863.2276" rx="21.40518" ry="14.970306" />
<rect style="fill:#f2f2f2;fill-opacity:1" id="rect4192-1-1-9-1" width="110.40601" height="60.835773" x="-976.95081" y="39.450497" transform="matrix(0,-1,1,0,0,0)" />
<rect style="fill:#f2f2f2;fill-opacity:1;stroke:#000000;stroke-opacity:0.98224852" id="rect4192-1-1-9-6" width="110.40601" height="60.835773" x="-758.51715" y="-46.298756" transform="matrix(0,-1,1,0,0,0)" />
<rect style="fill:#f2f2f2;fill-opacity:1;stroke:#000000;stroke-opacity:0.98224852" id="rect4192-1-1-9-6-0" width="110.40601" height="60.835773" x="-759.53809" y="53.483723" transform="matrix(0,-1,1,0,0,0)" />
</g>
</svg>
這是代碼錯誤是這個節點包含null爲什麼???
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Linq;
using System.Xml;
using System.Xml.XPath;
namespace ConsoleApplication2
{
class Program
{
static void Main(string[] args)
{
var doc = XDocument.Load("map.svg");
var node = doc.Descendants("rect").FirstOrDefault(cd => cd.Attribute("id").Value == "rect4182");
node.SetAttributeValue("style", ""fill:#f2f2f2;fill-opacity:1"");
doc.Save("Q317664.xml");
Console.ReadLine();
}
}
}
感謝#David莫頓它真的對我的作品 –
同樣同樣的問題節點爲null @大衛莫頓 –
是XML一樣嗎?如果這是您在上面發佈的內容,則更改爲一行即可。我測試了它。 –