简体   繁体   English

如何在C#中使用linq从xml文件中提取SVG子级html标签

[英]How to Extract SVG children html tag from xml file using linq in C#

I have a xml file in MVC web project as following: 我在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>

And I try to get the svg data from the item with the menuId == 10811010; 我尝试使用menuId == 10811010从项目中获取svg数据;

Here is my C# code: 这是我的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!");
    }

}

Why I can't get the svg data without embedding it into a <p> tag. 为什么不将svg数据嵌入<p>标记中就无法获取它。

The scope of this function is to extract only svg data without the <p> and inject it into a cshtml view. 该功能的范围是仅提取svg数据而不使用<p>并将其注入到cshtml视图中。

If you want to access the svg element then you need to take the namespace into account so define 如果要访问svg元素,则需要考虑名称空间,因此请定义

XNamespace svg = "http://www.w3.org/2000/svg";

then item.Element("p").Element(svg + "svg") gives you the root element of the embedded SVG document. 然后item.Element("p").Element(svg + "svg")为您提供嵌入式SVG文档的根元素。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM