XmaksasX
XmaksasX

Reputation: 91

How to add attributes in xml

please tell me, I have .xml

<Param>
<Mercedes/>
<Audi/>
<BMW/>
</Param>

I need to add attributes

<Param>
<Mercedes>
<Attr Name="Wheels" Value="true">
<Attr Name="Lights" Value=false>  
</Mercedes>
<Audi/>
<BMW/>
</Param>

XmlDocument doc = new XmlDocument();
doc.Load("Auto"); 
XmlNodeList el = doc.GetElementsByTagName("Mercedes");

I can't using GetElementsByTagName because name="Mercedes" always change.

Upvotes: 0

Views: 57

Answers (3)

DeshDeep Singh
DeshDeep Singh

Reputation: 1843

If you want to create a new xml file programmatically then try using this:

string path = System.AppDomain.CurrentDomain.BaseDirectory + "LogInUpdater.xml";
    XDocument doc;
                doc = new XDocument(
                                new XElement("LogUpdate",
                                new XElement("Id",
                                    new XAttribute("Id", IdL.Text)),
                                new XElement("Name",
                                    new XAttribute("Name", NameL.Text)),
                                new XElement("Password",
                                    new XAttribute("Password", txtPassword)),
                                new XElement("Department",
                                    new XAttribute("Department", DeptL.Text)),
                                new XElement("Time",
                                    new XAttribute("Time", x.LogTime.ToString())),
                                new XElement("TotalTime",
                                    new XAttribute("TotalTime", x.TotalTime.ToString())),
                                new XElement("Log",
                                    new XAttribute("Log", x.Log.ToString()))));
                SaveLoginInfoToDisk(doc);

otherwise if you have already existing xml file and you want to update it, try using this :

string path = System.AppDomain.CurrentDomain.BaseDirectory + "LogInUpdater.xml";
    XDocument  doc;
                doc = XDocument.Load(path);
                XElement ele = new XElement("LogUpdate",
                        new XElement("Id",
                            new XAttribute("Id", IdL.Text)),
                        new XElement("Name",
                            new XAttribute("Name", NameL.Text)),
                        new XElement("Password",
                            new XAttribute("Password", txtPassword.Password.ToString())),
                        new XElement("Department",
                            new XAttribute("Department", DeptL.Text)),
                        new XElement("Time",
                            new XAttribute("Time", x.LogTime.ToString())),
                        new XElement("TotalTime",
                            new XAttribute("TotalTime", x.TotalTime.ToString())),
                        new XElement("Log",
                            new XAttribute("Log", x.Log.ToString())));
                doc.Root.Add(ele);
                SaveLoginInfoToDisk(doc);

Upvotes: 1

codeninja.sj
codeninja.sj

Reputation: 4119

By using XmlDocument,

        XmlDocument doc = new XmlDocument();
        doc.LoadXml(xml);
        XmlNode mercedes = doc.SelectSingleNode("//Mercedes");

        XmlNode attr1 = doc.CreateNode(XmlNodeType.Element, "", "Attr", "");
        XmlAttribute name1 = doc.CreateAttribute("Name");
        name1.Value = "Wheels";
        XmlAttribute value1 = doc.CreateAttribute("Value");
        value1.Value = "true";
        attr1.Attributes.Append(name1);
        attr1.Attributes.Append(value1);
        mercedes.AppendChild(attr1);

        XmlNode attr2 = doc.CreateNode(XmlNodeType.Element, "", "Attr", "");
        XmlAttribute name2 = doc.CreateAttribute("Name");
        name2.Value = "Lights";
        XmlAttribute value2 = doc.CreateAttribute("Value");
        value2.Value = "false";
        attr2.Attributes.Append(name2);
        attr2.Attributes.Append(value2);
        mercedes.AppendChild(attr2);

Upvotes: 2

Charles Mager
Charles Mager

Reputation: 26213

I wouldn't use XmlDocument unless you have good reason to. Using LINQ to XML, for example:

var doc = XDocument.Parse(xml);

var mercedes = doc.Descendants("Mercedes").Single();

mercedes.Add(
    new XElement("Attr",
        new XAttribute("Name", "Wheels"),
        new XAttribute("Value", true)
        ),
    new XElement("Attr",
        new XAttribute("Name", "Lights"),
        new XAttribute("Value", false)
        )
    );     

Or for all cars:

foreach (var car in doc.Elements("Param").Elements())
{
    car.Add(
        new XElement("Attr",
            new XAttribute("Name", "Wheels"),
            new XAttribute("Value", true)
            ),
        new XElement("Attr",
            new XAttribute("Name", "Lights"),
            new XAttribute("Value", false)
            )
        );
}

Upvotes: 4

Related Questions