2012-10-03 22 views
1

我有一個XML字符串,這樣
更新XML標籤屬性的值在.NET

<root> 
Am trying <br id="9"/>to reorder the <br id="5"/>break 
lines <br id="10"/> attributes value 
</root> 

沒有辦法更改屬性的XML BR標籤ID的屬性值是在順序 這樣

<root> 
Am trying <br id="1"/>to reorder the <br id="2"/>break 
lines <br id="3"/> attributes value 
</root> 

回答

1

下面是使用一個例子LINQ TO XML

Dim doc as XElement = <root> 
Am trying <br id="9"/>to reorder the <br id="5"/>break 
lines <br id="10"/> attributes value 
</root> 

Dim index as Integer = 0 

For Each br In doc.<br> 
    index += 1 
    [email protected] = index 
Next 

這導致下面的輸出

<root> 
Am trying <br id="1" />to reorder the <br id="2" />break 
lines <br id="3" /> attributes value 
</root> 

另外,這裏是使用LAMBDA表達式的例子。

doc.<br>.ToList().ForEach(Sub(br) 
       index += 1 
       [email protected] = index 
       End Sub) 
+0

非常感謝你真的幫了我很多工作正確 –

+1

'doc.Elements(「BR」)'可以簡化爲'文檔。
'。 –

+0

@MarkHurd好點。我會更新樣本來反映這一點。 – Garett