1
这是我的xml文件如何更改标签名称和使用LINQ到XML
<tag>
<ImageObject Color="BlackWhite" FileRef="12.gif" Format="GIF" Rendition="HTML" Type="Linedraw" />
<ImageObject Color="BlackWhite" FileRef="32.gif" Format="GIF" Rendition="HTML" Type="Linedraw"/>
<ImageObject Color="BlackWhite" FileRef="3.gif" Format="GIF" Rendition="HTML" Type="Linedraw"/>
</tag>
,输出的东西与此类似
<tag>
<img src="12.gif" />
<img src="32.gif" />
<img src="3.gif" />
</tag>
到目前为止,这是我的代码获得属性。但我不能设置IMG的属性,因为我不知道如何检索的fileref
XElement rootImg = XElement.Parse(xml string variable);
IEnumerable<XElement> img =
from el in rootImg.Descendants("ImageObject").ToList()
where (string)el.Attribute("Format") != ""
select el;
foreach (XElement el in img)
{
el.Name = "img";
el.RemoveAttributes();
el.SetAttributeValue("src", "");
}
谢谢。你的代码工作 – codequery18