0

I need to add a following XML node in XML files.

<preference name='phonegap-version' value='cli-5.1.1' />

But I am getting like

<preference name="'phonegap-version' value='cli-5.1.1'" xmlns="" />

I have used the following code in C#.

XmlElement preference = doc.CreateElement("preference");
root.AppendChild(preference);
XmlAttribute newAttribute = doc.CreateAttribute("name");
newAttribute.Value="'phonegap-version' value='cli-5.1.1'";
preference.Attributes.Append(newAttribute);

Could you please resolve ? Thanks in advance...

grovesNL
  • 6,016
  • 2
  • 20
  • 32
  • [link]http://stackoverflow.com/questions/135000/how-to-prevent-blank-xmlns-attributes-in-output-from-nets-xmldocument – Thakur Jul 15 '15 at 05:43
  • If you are doing a lot of XML work, i have found the Xml library in Linq a great deal easier to use. XDocument, XElement and XAttribute. Imo (is that allowed on SO?) it clears up the clutter. – Nathan White Jul 15 '15 at 05:51

4 Answers4

1

The issue is that you're only creating one attribute (called name) then trying to fill it with multiple attributes. You only append that single attribute. Instead, you should create two attributes and append each of them.

XmlElement preference = doc.CreateElement("preference");
root.AppendChild(preference);

// create and append the attribute 'name'
XmlAttribute attributeName = doc.CreateAttribute("name");
attributeName.Value = "phonegap-version";
preference.Attributes.Append(attributeName);

// create and append the attribute 'value'
XmlAttribute attributeValue = doc.CreateAttribute("value");
attributeValue.Value = "cli-5.1.1";
preference.Attributes.Append(attributeValue);
grovesNL
  • 6,016
  • 2
  • 20
  • 32
  • Hi, Thanks for response. By trying your suggestion. I am getting like this I need like this – Parthiban S Jul 15 '15 at 05:21
  • If you need to remove the namespace attribute (`xmlns`), [there are many ways to do that](http://stackoverflow.com/a/24309268/3190758). Either single or double quotes should be fine for whichever XML handler you're using. Unfortunately, [I'm not sure there is an easy way to change the quote type from double to single](http://stackoverflow.com/questions/222819/save-attribute-value-of-xml-element-with-single-quotes-using-linq-to-xml). Whichever XML handler you're using should be able to handle this type of scenario regardless. – grovesNL Jul 15 '15 at 05:29
1

With XDocument, you can try this :

XDocument doc = XDocument.Load("doc.xml");
XElement el = new XElement("preference", new Object[] {new XAttribute("name", "phonegap-version"), new XAttribute("value", "cli-5.1.1")});
doc.Add(el);
doc.Save("doc.xml");
Neyoh
  • 623
  • 1
  • 11
  • 33
0

I found System.Xml.Linq easier to use. Try this:

        XDocument doc = XDocument.Load("yourpath.xml");
        XElement root = doc.Root;
        root.Add(new XElement("preference",
            new XAttribute("name", "'phonegap-version'"),
            new XAttribute("value", "'cli-5.1.1'")));
        doc.Save("yourpath.xml");
raduchept
  • 181
  • 4
  • 11
-1
foreach (XmlNodenode in preferance)
{

    // if element already there, it will override
    XmlAttribute newAttr = xdoc.CreateAttribute("value");
    newAttr.Value = "cli-5.1.1";
    node.Attributes.Append(newAttr);
}
Alex
  • 21,273
  • 10
  • 61
  • 73