Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add attributes to xml using XmlDocument in c# .net CF 3.5

I need to create an attribute "abc" with the prefix "xx" for an element "aaa". The following code adds the prefix but it also adds the namespaceUri to the element.

Required Output:

<mybody>
<aaa xx:abc="ddd"/>
<mybody/>

My Code:

  XmlNode node = doc.SelectSingleNode("//mybody");
  XmlElement ele = doc.CreateElement("aaa");

  XmlAttribute newAttribute = doc.CreateAttribute("xx","abc",namespace);              
  newAttribute.Value = "ddd";

  ele.Attributes.Append(newAttribute);

  node.InsertBefore(ele, node.LastChild);

The above code generates :

<mybody>
<aaa xx:abc="ddd" xmlns:xx="http://www.w3.org/1999/XSL/Transform"/>
<mybody/>

Desired output is

<mybody>
<aaa xx:abc="ddd"/>
<mybody/>

And the declaration of the "xx" attribute should be done in the root node like :

<ns:somexml xx:xsi="http://www.w3.org/1999/XSL/Transform"  xmlns:ns="http://x.y.z.com/Protocol/v1.0">

How can if get the output in the deisred format? If the xml is not in this desired format then it cannot be processed anymore..

Can anyone help?

Thanks, Vicky

like image 819
Vicky Avatar asked Aug 04 '10 12:08

Vicky


People also ask

How do you add an element to an attribute in XML?

Get the element node and use SetAttribute to add an attribute to the attribute collection of that element. Create an XmlAttribute node using the CreateAttribute method, get the element node, then use SetAttributeNode to add the node to the attribute collection of that element.

What are nodes and attributes in XML?

Node TypesElement Node − Every XML element is an element node. This is also the only type of node that can have attributes. Attribute Node − Each attribute is considered an attribute node. It contains information about an element node, but is not actually considered to be children of the element.

What are XML attributes explain with an example?

In the above example, XML element is text, the category is the attribute name and message is the attribute value, Attribute name and its value always appear in pair. The attribute name is used without any quotation but attribute value is used in single ( ' ' ) or double quotation ( ” ” ). Example 2: XML.

Does XML have attributes?

XML elements can have attributes, just like HTML. Attributes are designed to contain data related to a specific element.


1 Answers

I believe it's just a matter of setting the relevant attribute directly on the root node. Here's a sample program:

using System;
using System.Globalization;
using System.Xml;

class Test
{
    static void Main()
    {
        XmlDocument doc = new XmlDocument();
        XmlElement root = doc.CreateElement("root");

        string ns = "http://sample/namespace";
        XmlAttribute nsAttribute = doc.CreateAttribute("xmlns", "xx",
            "http://www.w3.org/2000/xmlns/");
        nsAttribute.Value = ns;
        root.Attributes.Append(nsAttribute);

        doc.AppendChild(root);
        XmlElement child = doc.CreateElement("child");
        root.AppendChild(child);
        XmlAttribute newAttribute = doc.CreateAttribute("xx","abc", ns);
        newAttribute.Value = "ddd";        
        child.Attributes.Append(newAttribute);

        doc.Save(Console.Out);
    }
}

Output:

<?xml version="1.0" encoding="ibm850"?>
<root xmlns:xx="http://sample/namespace">
  <child xx:abc="ddd" />
</root>
like image 64
Jon Skeet Avatar answered Sep 21 '22 14:09

Jon Skeet