如何在xml中添加属性

本文关键字:添加 属性 xml | 更新日期: 2023-09-27 18:03:04

请告诉我,我有。xml

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

我需要添加属性

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

我不能使用GetElementsByTagName因为name="Mercedes"总是改变。

如何在xml中添加属性

除非你有很好的理由,否则我不会使用XmlDocument。对XML使用LINQ,例如:

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

或所有汽车:

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

使用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);

如果您想以编程方式创建一个新的xml文件,请尝试使用以下命令:

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

否则,如果您已经存在XML文件,并且您想要更新它,请尝试使用:

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