gpt4 book ai didi

c# - 使用 XDocument 和 XElement 如何在 C# 中向 XML 添加 XAttribute 和值

转载 作者:太空宇宙 更新时间:2023-11-03 23:39:55 26 4
gpt4 key购买 nike

我正在尝试复制:

    <gcf>
<cbxDecOnly Type="Boolean">False</cbxDecOnly>
<cbxFormName Type="String" />
<txtCustomerCellPhonePart2 Type="String">5236</txtCustomerCellPhonePart2>
<txtCustomerCellPhonePart1 Type="String">533</txtCustomerCellPhonePart1>
....
</gcf>

到目前为止我有:

var xdoc = new XDocument(
new XDeclaration("1.0", "utf-8", null),
new XElement("gcf",
new XElement("cbxDecOnly", new XAttribute("Type", "Boolean")),
new XElement("cbxFormName", oGSFE.TextBoxClientName),
new XElement("txtCustomerCellPhonePart2", oGSFE.TextBoxDealSearch),
new XElement("txtCustomerCellPhonePart1 ", oGSFE.DropDownListFIManager)
)
);

我不知道的是如何同时向 XML 元素添加 XAttribute 和值 <cbxDecOnly Type="Boolean">False</cbxDecOnly>

最佳答案

以相同的方式为您的 txtCustomerCellPhonePart2 等节点提供值 - 通过将字符串值包含为 element's params content[] 之一:

var xdoc = new XDocument(
new XDeclaration("1.0", "utf-8", null),
new XElement("gcf",
new XElement("cbxDecOnly", "False", new XAttribute("Type", "Boolean")),
new XElement("cbxFormName", oGSFE.TextBoxClientName),
new XElement("txtCustomerCellPhonePart2", oGSFE.TextBoxDealSearch),
new XElement("txtCustomerCellPhonePart1", oGSFE.DropDownListFIManager)
)
);

content[] 中提供的任何 string 类型的值都将合并到元素的值中,任何 XAttribute 类型的值都将创建属性XElement 类型的任何值都将成为子项。

关于c# - 使用 XDocument 和 XElement 如何在 C# 中向 XML 添加 XAttribute 和值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29413714/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com