2016-04-15 35 views
0

在XML多重屬性下面是我的XML(或讓我們說SVG):刪除使用的XElement

<svg xmlns:sodipodi="http://sodipodi.sourceforge.net/DTD/sodipodi-0.dtd" 
xmlns:inkscape="http://www.inkscape.org/namespaces/inkscape" 
inkscape:version="0.48.2 r9819" 
sodipodi:docname="android.svg" viewbox="0 0 32 54"> 
[Some nodes below] 
</svg> 

從這一個,我想在名稱中包含任何inkscapesodipodi(在這種情況下,除去所有屬性,應保留屬性爲viewbox

希望你能幫助我在此謝謝

回答

2

這是一個可能是這樣的:。!

var raw = @"<svg xmlns:sodipodi='http://sodipodi.sourceforge.net/DTD/sodipodi-0.dtd' 
xmlns:inkscape='http://www.inkscape.org/namespaces/inkscape' 
inkscape:version='0.48.2 r9819' 
sodipodi:docname='android.svg' viewbox='0 0 32 54'> 
[Some nodes below] 
</svg>"; 
var svg = XElement.Parse(raw); 
var keywords = new[] { "inkscape", "sodipodi" }; 
svg.Attributes() 
    .Where(o => keywords.Any(k => o.Name.ToString().Contains(k))) 
    .Remove(); 
Console.WriteLine(svg.ToString()); 

dotnetfiddle demo

輸出:

<svg viewbox="0 0 32 54"> 
[Some nodes below] 
</svg> 
+0

有沒有辦法在VB做這個或C#,因爲我打算做它的動態。 – Musikero31

+0

所以C#是其中一個選項嗎?以上是在C#中完成的,有什麼問題?我不明白你的意思是* dynamic * here .. – har07

+0

哦!它看起來像JavaScript ...或者我想我很困惑。 – Musikero31