1

I have an XML as below. In this XML all the attributes are available as elements.

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

I need convert this XML as:

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

Basically I need to convert the elements to attributes. How do I do this using c#?

4
  • are u sure you are going to modify the source file ? then why c# tag? Commented May 25, 2017 at 9:30
  • Yes. The source file has to be modified. I need to do this using c#. Commented May 25, 2017 at 9:59
  • what you have tried so far? Commented May 25, 2017 at 10:00
  • Did you got your solution? See my answer. It generates output as you expected Commented May 25, 2017 at 11:46

2 Answers 2

1

Try withe below one. it generates output as you expected.

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();
        }
    }
}

OUTPUT

enter image description here

Sign up to request clarification or add additional context in comments.

Comments

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();

}

2 Comments

you have missed ID="001" attribute for the <Dress> root element
@uɐpuɐɥƆ good spot! I don't think this would need a huge change to fix though.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.