I want to edit xml file

泄露秘密 提交于 2019-12-23 05:49:06

问题


I have modified the structure of d xml file. i want to edit value of visible


回答1:


You can use such code pattern:

bool foobar()
    {
        XmlDocument doc = new XmlDocument();
        try
        {
            doc.Load(FileName);
            XmlNodeList ns = doc.SelectNodes("a/d/e/f");
            if (ns.Count == 1)
            {

                    ns[0].Attributes["visible"].Value = true;
                    doc.Save(FileName);
                    return (true);
            }
            else
                return (false);
        }
        catch (Exception e)
        {
            return (false);
        }
    }



回答2:


Well, LINQ to XML makes it very easy to manipulate XML documents, assuming they're small enough to be sensibly loaded into memory.

For example:

var doc = XDocument.Load("Foo.xml");
foreach (var element in doc.Descendants("c"))
{
    element.SetAttributeValue("value", "bb");
}
doc.Save("Bar.xml");

Now that will set the value attribute for every c element. It's not clear whether or not that's what you want. If it's not, please edit your question to make it more specific.



来源:https://stackoverflow.com/questions/4208438/i-want-to-edit-xml-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!