在 LINQ to XML 中设置属性值
我是 LINQ to XML 的新手。我有两个变量 one 和 two,我想在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以使用
XElement.SetAttributeValue( )
方法:You can use the
XElement.SetAttributeValue()
method:如果您要经常进行此调用,您不妨将其放入辅助方法中,例如:
然后在 main.c 中调用以下内容:
If you're going to be making this call often, you might as well put it into a helper method such as:
and then call the following in your main.
这些人提供的答案是否有任何原因不会写入 XML?没有例外,一切看起来都很好,除了它不写入文件。
这是我的代码
基本上与上面相同
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
Basically it is same as above