2017-05-25 149 views
1

我有一個XML,如下所示。在這個XML中,所有的屬性都可以作爲元素使用。如何使用c將XML元素轉換爲屬性#

<Dress> 
    <ID>001</ID> 
    <shirts> 
     <product> 
      <ID>345</ID> 
      <Name>tee</Name> 
      <Serial>5678</Serial> 
     </product> 
     <product> 
      <ID>456</ID> 
      <Name>crew</Name> 
      <Serial>4566</Serial> 
     </product>  
    </shirts> 
    <pants> 
     <product> 
      <ID>123</ID> 
      <Name>jeans</Name> 
      <Serial>1234</Serial> 
      <Color>blue</Color> 
     </product> 
     <product> 
      <ID>137</ID> 
      <Name>skirt</Name> 
      <Serial>3455</Serial> 
      <Color>black</Color> 
     </product> 
    </pants> 
</Dress> 

我需要轉換這個XML爲:

<Dress ID="001"> 
    <shirts> 
     <product ID="345" Name="tee" Serial="5678"/> 
     <product ID="456" Name="crew" Serial="4566"/> 
    </shirts> 
    <pants> 
     <product ID="123" Name="jeans" Serial="1243" Color="blue"/> 
     <product ID="123" Name="skirt" Serial="3455" Color="black"/> 
    </pants> 
</Dress> 

基本上我需要的元素轉換爲屬性。我如何使用C#做到這一點?

+0

是U相信您一定會修改源文件?那麼爲什麼C#標籤? –

+0

是的。源文件必須修改。我需要使用c#來做到這一點。 – Vidhya

+0

你到目前爲止嘗試過什麼? –

回答

1

試試下面的一個。它會按照您的預期生成輸出。

using System; 
using System.Linq; 
using System.Xml.Linq; 

namespace CangeOneXmlToAnotherXmlConsoleApp 
{ 
    class Program 
    { 
     static void Main(string[] args) 
     { 
      var sourceXml = @"<Dress> 
           <ID>001</ID> 
           <shirts> 
            <product> 
             <ID>345</ID> 
             <Name>tee</Name> 
             <Serial>5678</Serial> 
            </product> 
            <product> 
             <ID>456</ID> 
             <Name>crew</Name> 
             <Serial>4566</Serial> 
            </product>  
           </shirts> 
           <pants> 
            <product> 
             <ID>123</ID> 
             <Name>jeans</Name> 
             <Serial>1234</Serial> 
             <Color>blue</Color> 
            </product> 
            <product> 
             <ID>137</ID> 
             <Name>skirt</Name> 
             <Serial>3455</Serial> 
             <Color>black</Color> 
            </product> 
           </pants> 
          </Dress>"; 
      var xmlDoc = XDocument.Parse(sourceXml); 
      //Remove the ID element 
      var firstChildNodeVal = ((XElement)((XContainer)xmlDoc.FirstNode).FirstNode).Value; 
      xmlDoc.Descendants("ID").Remove(); 
      //Add an attribute(ID) with value to the root element 
      xmlDoc.Root.SetAttributeValue("ID", firstChildNodeVal); 
      //Define the new elements to be available inside the root element 
      var elemetsToBeFormatted = new string[] { "shirts", "pants" }; 
      //Loop it and add the elements inside root element 
      foreach (var item in elemetsToBeFormatted) 
      { 
       var aitem = xmlDoc.Root.Elements(item).Elements("product").ToList(); 
       aitem.ForEach(p => p.Elements().ToList().ForEach(e => { p.SetAttributeValue(e.Name, e.Value); e.Remove(); })); 
      } 
      var expectedXml = xmlDoc.ToString(); 
      Console.WriteLine(expectedXml); 
      Console.Read(); 
     } 
    } 
} 

輸出

enter image description here

+0

@Chandan,你的代碼工作得很好。非常感謝!!! – Vidhya

0
private static void ReplaceElementsWithAttributes() 
{ 
    string xmlData = @"<Dress> 
          <ID>001</ID> 
          <shirts> 
           <product> 
            <ID>345</ID> 
            <Name>tee</Name> 
            <Serial>5678</Serial> 
           </product> 
           <product> 
            <ID>456</ID> 
            <Name>crew</Name> 
            <Serial>4566</Serial> 
           </product>  
          </shirts> 
          <pants> 
           <product> 
            <ID>123</ID> 
            <Name>jeans</Name> 
            <Serial>1234</Serial> 
            <Color>blue</Color> 
           </product> 
           <product> 
            <ID>137</ID> 
            <Name>skirt</Name> 
            <Serial>3455</Serial> 
            <Color>black</Color> 
           </product> 
          </pants> 
         </Dress>"; 

    var doc = XDocument.Parse(xmlData); 
    var replaceElementTargets = new string[] { "shirts", "pants" }; 

    foreach (var target in replaceElementTargets) 
    { 
     var product = doc.Root.Elements(target).Elements("product").ToList(); 
     product.ForEach(p => p.Elements().ToList().ForEach(e => { p.SetAttributeValue(e.Name, e.Value); e.Remove(); })); 
    } 

    var outputXML = doc.ToString(); 

} 
+1

您錯過了ID爲「001」屬性的根元素 –

+0

@uɐpuɐɥƆ好點!我不認爲這需要一個巨大的修改才能解決。 –

相關問題