Convert inline XML node to nested nodes in asp.net using C #

I have an XML file, for example:

<?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> 

For some reason, I need to create child and nested nodes from Element node attributes .
The result I want:

 <?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> 

How can i do this?
Or any idea, link, article ...

Thanks.

+5
source share
4 answers

This is one of the possible ways; for each <Element> attribute, add the corresponding child element, and then remove all attributes afterwards:

 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

For more complex XML conversions, take a look at XSLT.

+4
source

Here is the XSL stylesheet encoded for your exact file. Hope this is what you need.

You can use the XSL Transfrom online tool like this or use a script.

Here is the XSL:

 <?xml version="1.0"?> <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> <xsl:output method="xml" indent="yes"/> <xsl:template match="/"> <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,"> <xsl:for-each select="/LayoutControl/LayoutGroup/Element"> <Element> <ID><xsl:value-of select="@ID"/></ID> <HorizontalAlignment><xsl:value-of select="@HorizontalAlignment"/></HorizontalAlignment> <VerticalAlignment><xsl:value-of select="@VerticalAlignment"/></VerticalAlignment> <Width><xsl:value-of select="@Width"/></Width> <Height><xsl:value-of select="@Height"/></Height> <Label><xsl:value-of select="@Label"/></Label> <Background><xsl:value-of select="@Background"/></Background> <ContentName><xsl:value-of select="@ContentName"/></ContentName> <Type><xsl:value-of select="@Type"/></Type> </Element> </xsl:for-each> </LayoutGroup> </LayoutControl> </xsl:template> </xsl:stylesheet> 
+1
source

You can use Linq .

  XDocument doc = XDocument.Load(filepath); foreach(var element in doc.Descendants("Element") .Select(x=> new {element =x, attributes= x.Attributes()})) { var attributes = element.attributes.ToList(); element.element.RemoveAttributes(); // remove all attributes. foreach(XAttribute attribute in attributes) { element.element.Add(new XElement(attribute.Name, attribute.Value)); // Convert each attribute to an element } } 

Output

  <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> 

Fiddle Demo

+1
source

First get the xml node. Then go through the attributes, convert the attributes to a new node, add to the xml node and remove the attribute, you can do something like this:

  XmlDocument xmldoc = new XmlDocument(); xmldoc.LoadXml(xmlstring); XmlNode elementNode = xmldoc.SelectSingleNode("LayoutControl/LayoutGroup/Element"); if (elementNode != null) { foreach (XmlAttribute attribute in elementNode.Attributes) { XmlElement elementchild = xmldoc.CreateElement(attribute.Name); elementchild.InnerText = attribute.Value; elementNode.AppendChild(elementchild); } } elementNode.Attributes.RemoveAll(); 

Hope this helps

0
source

Source: https://habr.com/ru/post/1247767/


All Articles