我想编辑xml文件

发布于 2024-10-02 19:45:42 字数 99 浏览 0 评论 0原文

alt text

我修改了 d xml 文件的结构。我想编辑可见的值

alt text

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

滥情稳全场 2024-10-09 19:45:42

那么,LINQ to XML 使操作 XML 文档变得非常容易,前提是它们足够小,可以合理地加载到内存中。

例如:

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

现在将为每个 c 元素设置value 属性。目前尚不清楚这是否是您想要的。如果不是,请编辑您的问题以使其更加具体。

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.

溺ぐ爱和你が 2024-10-09 19:45:42

您可以使用这样的代码模式:

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

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);
        }
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文