如何在xml

时间:2015-07-16 09:56:45

标签: c# xml wpf

请告诉我,我有.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因为名字=&#34;梅赛德斯&#34;永远改变。

2 个答案:

答案 0 :(得分:2)

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

答案 1 :(得分:1)

如果要以编程方式创建新的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);