c# - 使用 C# 将内联 XML 节点转换为 asp.net 中的嵌套节点

标签 c# asp.net xml

我有一个如下所示的 XML 文件:

<?xml version="1.0" encoding="utf-8" ?>
<LayoutControl ID="rootlyt" Type="LayoutControl">
  <LayoutGroup ID="lgp8" Header="PersonalInfo" IsCollapsed="False" IsLocked="False" Orientation="Vertical" View="GroupBox" HorizontalAlignment="Left" VerticalAlignment="Top" Width="380" Height="295" Type="GroupItem" Properties="IsCollapsible=False,IsCollapsed=False,IsLocked=False,">
    <Element ID="layout2" HorizontalAlignment="Left" VerticalAlignment="Top" Width="300" Height="25" Label="Name" Background="#00FFFFFF" ContentName="txt2" Type="TextEdit" />
  </LayoutGroup>
</LayoutControl>  

出于某些原因,我需要从 Element 节点 attributes 创建子节点和嵌套节点。
我想要的输出是:

<?xml version="1.0" encoding="utf-8" ?>
<LayoutControl ID="rootlyt" Type="LayoutControl">
  <LayoutGroup ID="lgp8" Header="PersonalInfo" IsCollapsed="False" IsLocked="False" Orientation="Vertical" View="GroupBox" HorizontalAlignment="Left" VerticalAlignment="Top" Width="380" Height="295" Type="GroupItem" Properties="IsCollapsible=False,IsCollapsed=False,IsLocked=False,">
    <Element >
      <ID>layout2</ID>
      <HorizontalAlignment>Left</HorizontalAlignment>
      <VerticalAlignment>Top</VerticalAlignment>
      <Width>300</Width>
      <Height>25</Height>
      <Label>Name</Label>
      <Background>#00FFFFFF</Background>
      <ContentName>txt2</ContentName>
      <Type>TextEdit</Type>
    </Element>
  </LayoutGroup>
</LayoutControl>  

我该怎么做?
或任何想法、引用、文章...

谢谢。

最佳答案

这是一种可能的方式;对于每个 <Element>的属性添加一个相应的子元素,然后删除所有属性:

var raw = @"<LayoutControl ID='rootlyt' Type='LayoutControl'>
  <LayoutGroup ID='lgp8' Header='PersonalInfo' IsCollapsed='False' IsLocked='False' Orientation='Vertical' View='GroupBox' HorizontalAlignment='Left' VerticalAlignment='Top' Width='380' Height='295' Type='GroupItem' Properties='IsCollapsible=False,IsCollapsed=False,IsLocked=False,'>
    <Element ID='layout2' HorizontalAlignment='Left' VerticalAlignment='Top' Width='300' Height='25' Label='Name' Background='#00FFFFFF' ContentName='txt2' Type='TextEdit' />
  </LayoutGroup>
</LayoutControl>";

var doc = XDocument.Parse(raw);
foreach(var element in doc.Descendants("Element"))
{
    //add a series of child elements according to existing attributes
    element.Add(
        element.Attributes()
               .Select(attribute => new XElement(attribute.Name.LocalName, attribute.Value))
    );

    //remove the attributes
    element.Attributes().Remove();
}

Console.WriteLine(doc.ToString());

dotnetfiddle demo

对于更复杂的 XML 转换,请查看 XSLT。

关于c# - 使用 C# 将内联 XML 节点转换为 asp.net 中的嵌套节点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36807775/

相关文章:

c# - 在 ASP.NET Core 2.2 中将部分 View 呈现为 HTML 字符串

asp.net - AJAX、回发和浏览器刷新

asp.net - 从 asp.net 的内容页中的母版页中查找 UnorderedList <UL> 控件

asp.net - 获取下拉列表中选中项的值

针对 XSD : The element xxx has invalid child element yyy in namespace zzz 的 XML 验证

xml - 具有可选条件的XPath查询(仅在标记存在时才应用条件)

c# - 从javascript触发服务器端事件

c# - 如何剪掉重叠区域?

c# - C#中异步调用方法

xml - 将 fop 表向右移动