我在MVC web项目中有一个xml文件,如下所示:
<Navigation>
<item>
<CoreId>1010</CoreId>
<p>
<svg width="640" height="480" xmlns="http://www.w3.org/2000/svg" xmlns:svg="http://www.w3.org/2000/svg">
<!-- Created with SVG-edit - http://svg-edit.googlecode.com/ -->
<g>
<title>Layer 1</title>
<rect height="170" width="258" y="136.67" x="188" stroke-width="5" stroke="#000000" fill="#ff0000" id="svg_1"/>
</g>
</svg>
</p>
<Privilege>ADMIN</Privilege>
<MenuID>10811010</MenuID>
<ParentMenuID>1081</ParentMenuID>
<ResourceKey>AP_Template</ResourceKey>
</item>并尝试用menuId == 10811010从项目中获取svg数据;
下面是我的C#代码:
public string GetVPFCore(int vpfCore)
{
VPFCore _localitem = new VPFCore();
try
{
var element = from item in xmlController.Root.Descendants("item")
where (string)item.Element("MenuID") == vpfCore.ToString()
select new
{
_data = item.Element("p").ToString()
};
foreach (var t in element)
{
_localitem.Data = t._data;
}
return _localitem.Data;
}
catch (Exception)
{
throw new IOException("The XML item was not found!");
}
}为什么我不能在不嵌入<p>标签的情况下获取svg数据。
该函数的作用范围是只提取没有<p>的svg数据,并将其注入到cshtml视图中。
发布于 2015-04-25 01:24:30
如果想要访问svg元素,则需要考虑名称空间,因此定义
XNamespace svg = "http://www.w3.org/2000/svg";然后,item.Element("p").Element(svg + "svg")为您提供嵌入式SVG文档的根元素。
https://stackoverflow.com/questions/29853613
复制相似问题