在 LINQ to XML 中设置属性值

发布于 2024-12-02 11:30:40 字数 854 浏览 3 评论 0原文

我是 LINQ to XML 的新手。我有两个变量 onetwo,我想在 XML 的属性中设置这些变量值。

 static void Main(string[] args)
    {
        string one = "first";
        string two = "Second";

        XDocument doc = XDocument.Load(test.xml);

    }

XML

    <Root>
  <Details XIndex="One" Index="">
    <abc></abc>
  </Details>
  <Details XIndex="Two" Index="">
    <xyz></xyz>
  </Details>
</Root>

现在请告诉我如何在详细信息节点的索引属性中设置一个和两个变量值。

示例 - 我想要以下输出。

<Root>
  <Details XIndex="One" Index="First">
    <abc></abc>
  </Details>
  <Details XIndex="Two" Index="Second">
    <xyz></xyz>
  </Details>
</Root>

请告诉我。

提前致谢。

I am newbie in LINQ to XML.I have two variable one and two and I want to set these variable values in attribute in XML.

 static void Main(string[] args)
    {
        string one = "first";
        string two = "Second";

        XDocument doc = XDocument.Load(test.xml);

    }

XML

    <Root>
  <Details XIndex="One" Index="">
    <abc></abc>
  </Details>
  <Details XIndex="Two" Index="">
    <xyz></xyz>
  </Details>
</Root>

Now please tell me how I can set One and two variables value in Index attribute in details node.

Example - I want below output.

<Root>
  <Details XIndex="One" Index="First">
    <abc></abc>
  </Details>
  <Details XIndex="Two" Index="Second">
    <xyz></xyz>
  </Details>
</Root>

Please tell me.

Thanks in advance.

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

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

发布评论

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

评论(3

情话墙 2024-12-09 11:30:40

您可以使用 XElement.SetAttributeValue( )方法:

var element = doc.Elements("Details")
                 .Single(x=>x.Attribute("XIndex").Value=="One");

element.SetAttributeValue("Index", "First");

You can use the XElement.SetAttributeValue() method:

var element = doc.Elements("Details")
                 .Single(x=>x.Attribute("XIndex").Value=="One");

element.SetAttributeValue("Index", "First");
软甜啾 2024-12-09 11:30:40

如果您要经常进行此调用,您不妨将其放入辅助方法中,例如:

private static void SetValueToDetailElement(XDocument doc, string xIndex, string value)
{
  var detail = doc.Elements("Details").SingleOrDefault(x=>x.Attribute("XIndex").Value==xIndex);
  if(detail != null)
     detail.SetAttributeValue("Index", value);
}

然后在 main.c 中调用以下内容:

SetValueToDetailElement(doc, "One", "First");
SetValueToDetailElement(doc, "Two", "Second");

If you're going to be making this call often, you might as well put it into a helper method such as:

private static void SetValueToDetailElement(XDocument doc, string xIndex, string value)
{
  var detail = doc.Elements("Details").SingleOrDefault(x=>x.Attribute("XIndex").Value==xIndex);
  if(detail != null)
     detail.SetAttributeValue("Index", value);
}

and then call the following in your main.

SetValueToDetailElement(doc, "One", "First");
SetValueToDetailElement(doc, "Two", "Second");
jJeQQOZ5 2024-12-09 11:30:40

这些人提供的答案是否有任何原因不会写入 XML?没有例外,一切看起来都很好,除了它不写入文件。

这是我的代码

string file = "c:/message.xml";
XDocument d = XDocument.Load(file);

SetValueToIdElement(d, id, value);

private static void SetValueToIdElement(XDocument doc, string Id, string value)
        {
            var detail = doc.Elements("context").SingleOrDefault(x => x.Attribute("id").Value == Id);
            if (detail != null)
                detail.SetAttributeValue("value", value);
        }

基本上与上面相同

Is there any reason why answers provided by these guys wont write to XML? There is no exception, everything looks fine except it does not write to file.

Here's mine code

string file = "c:/message.xml";
XDocument d = XDocument.Load(file);

SetValueToIdElement(d, id, value);

private static void SetValueToIdElement(XDocument doc, string Id, string value)
        {
            var detail = doc.Elements("context").SingleOrDefault(x => x.Attribute("id").Value == Id);
            if (detail != null)
                detail.SetAttributeValue("value", value);
        }

Basically it is same as above

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