本文介绍了将属性添加到XML文件Linq C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想为我的所有XML文件添加一个Test(通用)属性.这样,当我想测试它们时就可以将其用作通用属性.

I wanna add a Test (common) attribute to all my XML files. So that I could use it as a common attribute when I wanna test them.

我尝试了CreateAttribute,但是Linq无法识别

I tried CreateAttribute but Linq dosen't recognize it

我尝试了"xElement.Add(new XAttribute("Test",value));但是也没有用有什么建议吗?

I tried "xElement.Add(new XAttribute("Test", value));" but it also didn't workAny Suggestions?

谢谢

例如,这里是一个代码

    public void updateXmlFile(string strFileName)
    {
        XDocument oXDoc = XDocument.Load(strFileName);
        XElement oDcElement = oXDoc.Root.FirstNode as XElement;

        //Generate a Unique String to replace the original attribute value
        string newValue = GetUniqueKey();

        //oDcElement.Add(new XAttribute("Test", newValue)); /*NullReferenceException*/

        oDcElement.Attribute("Remark").Value = newValue; //This changes only the Remark Attribute
        oXDoc.Save(strFileName);                         //which isn't available in all XMLs

    }

我想为通过此方法传递给XML的XML添加一个附加的公共值,并为其赋予一个随机值

I wanna add an additional, common value to the XMLs I pass through this method and give it a random value

我的目标是能够对XML进行更改,然后将其与另一个文件夹中的原始副本进行比较

My goal is to be able to make changes on an XML then compare it against the original copy in another folder

推荐答案

使用SetAttribute:

Use SetAttribute:

oDcElement.SetAttributeValue("Test", newValue);

这篇关于将属性添加到XML文件Linq C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-17 18:36
查看更多