2016-04-23 16 views
5

Ho un file XML, come di seguito:Convertire nodo XML inline a nodi nidificati in asp.net utilizzando C#

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

Per alcune ragioni, ho bisogno di creare bambino e nodi nidificati dal Element nodo attributes .
L'output che voglio è:

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

Come posso farlo?
O qualsiasi idea, riferimento, articolo ...

Grazie.

risposta

4

Questo è un modo possibile; per ogni <Element> 'attribuiscono s aggiungere un corrispondente elemento figlio, e quindi rimuovere tutti gli attributi in seguito:

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

Per le trasformazioni più complesse di XML, guardare in XSLT.

+0

Yep, che funziona fine.Thank voi :) –

0

Prendi il nodo XML prima, poi attraversare gli attributi, di convertire gli attributi per un nuovo nodo, aggiungere al nodo xml e rimuovere l'attributo, si può fare qualcosa di simile:

 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 Questa può help

1

Ecco un foglio di stile XSL per il file esatto. Speriamo che questo sia ciò di cui hai bisogno.

È possibile utilizzare uno XSL Transfrom tool like this one online o utilizzare uno script.

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

Si potrebbe utilizzare 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 
     } 
    } 

uscita

<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

+0

la soluzione è uno dei modi migliori, grazie. –

+0

Nessun problema, felice che ti abbia aiutato :-) –