john
john

Reputation: 1767

Customizing an XML element using LINQ To XML

I have the following code:

  XNamespace testNM = "urn:lst-emp:emp";
                XDocument xDoc;
                string path = "project_data.xml";
                if (!File.Exists(path))
                {
                    xDoc = new XDocument(
                               new XDeclaration("1.0", "UTF-16", null),
                               new XElement(testNM + "Test")
                               );
                }
                else
                {
                    xDoc = XDocument.Load(path);
                }

                var element = new XElement("key",
                                        new XElement("Type", type),
                                        new XElement("Value", value));
                xDoc.Element(testNM + "Test").Add(element);

                // Save to Disk
                xDoc.Save(path);

which produces an output in the XML file like this:

<?xml version="1.0" encoding="utf-16"?>
<Test xmlns="urn:lst-emp:emp">
  <key xmlns="">
    <Type>String</Type>
    <Value>somestring</Value>
  </key>
</Test>

How can I get an output like this in the XML file instead?

<?xml version="1.0" encoding="utf-16"?>
<Tests xmlns="urn:lst-emp:emp">
  <key name="someString">
    <Type>String</Type>
    <Value>somestring</Value>
  </key >
</Tests>

Note its only the 3rd line that has changed in the XML file.

Upvotes: 0

Views: 98

Answers (3)

toadflakz
toadflakz

Reputation: 7934

To provide a complete version of Bilyukov's answer, this should produce the output expected. Obviously substitute the "someString" static string with a variable populated as you wish. Changes include using XName.Get(string, string) to create the appropriate XName objects for the XElement constructors.

 XNamespace testNM = "urn:lst-emp:emp";
            XDocument xDoc;
            string path = "project_data.xml";
            if (!File.Exists(path))
            {
                xDoc = new XDocument(
                           new XDeclaration("1.0", "UTF-16", null),
                           new XElement(XName.Get("Tests", testNM.NamespaceName))
                           );
            }
            else
            {
                xDoc = XDocument.Load(path);
            }

            var element = new XElement(XName.Get("key", testNM.NamespaceName),
                                    new XAttribute("name", "someString"),
                                    new XElement("Type", type),
                                    new XElement("Value", value));
            xDoc.Element(XName.Get("Tests", testNM.NamespaceName)).Add(element);

            // Save to Disk
            xDoc.Save(path);

Upvotes: 2

har07
har07

Reputation: 89315

Your XML has default namespace. Descendants of the element where default namespace declared is considered in the same default namespace, unless it is explicitly declared with different namespace. That's why you need to use the same XNamespace for <key> element. :

var element = new XElement(testNM +"key",
                           new XAttribute("name", "someString"),
                           new XElement(testNM +"Type", type),
                           new XElement(testNM +"Value", value));

Upvotes: 1

Georgi Bilyukov
Georgi Bilyukov

Reputation: 627

You can do it this way:

var element = new XElement("key",
                           new XAttribute("name", "someString"),
                           new XElement("Type", "type"),
                           new XElement("Value", "value"));

Upvotes: 3

Related Questions