使用XMLDocument C#xml添加多个节点
我正在尝试在XML中添加多个元素/节点(发票节点) 以下是xml结构:(必需的输出) 上面的代码生成以下代码:使用XMLDocument C#xml添加多个节点,c#,xml,C#,Xml,我正在尝试在XML中添加多个元素/节点(发票节点) 以下是xml结构:(必需的输出) 上面的代码生成以下代码: <Request> <Operation>Testing</Operation> <Count>2</Count> <Params> <Invoice> <Field name="CustomerNo" value="10000" /> <F
<Request>
<Operation>Testing</Operation>
<Count>2</Count>
<Params>
<Invoice>
<Field name="CustomerNo" value="10000" />
<Field name="Email" value="testemail@yahoo.com" />
<Field name="Invoice" value="1002" />
</Invoice>
</Params>
</Request>
<Request>
<Operation>Testing</Operation>
<Count>2</Count>
<Params>
<Invoice>
<Field name="CustomerNo" value="10000" />
<Field name="Email" value="testemail@yahoo.com" />
<Field name="Invoice" value="1003" />
</Invoice>
</Params>
</Request>
测试
2.
测试
2.
我只想根据数组中的发票数量循环发票节点部分。我知道我正在循环我的XML的整个结构,但是我找不到一种方法将我的循环插入到XMLDocument中
任何帮助都将不胜感激
谢谢 您可以用LINQ替换循环:
XDocument xDocument = new XDocument(
new XDeclaration("1.0", "UTF-8", null),
new XElement("Request",
new XElement("Operation", "Testing"),
new XElement("Count","2"),
new XElement("Params", invoices.Select(x =>
new XElement("Invoice",
new XElement("Field",
new XAttribute("name","CustomerNo"),
new XAttribute("value","10000")),
new XElement("Field",
new XAttribute("name","Email"),
new XAttribute("value","testemail@yahoo.com")),
new XElement("Field",
new XAttribute("name","Invoice"),
new XAttribute("value",x)))))));
问题是您正在循环中迭代完整的文档
int[] invoice = new int[] {1002,1003};
List<XElement> eleList = new List<XElement>();
foreach (int i in invoice)
{
eleList.Add(
new XElement("Invoice",
new XElement("Field",
new XAttribute("name","CustomerNo"),
new XAttribute("value","10000")),
new XElement("Field",
new XAttribute("name","Email"),
new XAttribute("value","testemail@yahoo.com")),
new XElement("Field",
new XAttribute("name","Invoice"),
new XAttribute("value",i)))
);
}
XDocument xDocument = new XDocument(
new XDeclaration("1.0", "UTF-8", null),
new XElement("Request",
new XElement("Operation", "Testing"),
new XElement("Count","2"),
new XElement("Params", eleList)));
int[]发票=新的int[]{10021003};
List eleList=新列表();
foreach(发票中的int i)
{
eleList.Add(
新XElement(“发票”,
新XElement(“字段”,
新XAttribute(“名称”、“客户号”),
新XAttribute(“价值”、“10000”),
新XElement(“字段”,
新XAttribute(“姓名”、“电子邮件”),
新XAttribute(“值”testemail@yahoo.com")),
新XElement(“字段”,
新XAttribute(“名称”、“发票”),
新特性(“值”,i)))
);
}
XDocument XDocument=新XDocument(
新XDeclaration(“1.0”,“UTF-8”,空),
新XElement(“请求”,
新XElement(“操作”、“测试”),
新元素(“计数”、“2”),
新元素(“参数”,元素列表));
Hi@Selman,它确实有效!哇。我没想到你能做到!非常感谢你的帮助!嗨@ManishM,这是问题的另一种解决方案!非常感谢。
XDocument xDocument = new XDocument(
new XDeclaration("1.0", "UTF-8", null),
new XElement("Request",
new XElement("Operation", "Testing"),
new XElement("Count","2"),
new XElement("Params", invoices.Select(x =>
new XElement("Invoice",
new XElement("Field",
new XAttribute("name","CustomerNo"),
new XAttribute("value","10000")),
new XElement("Field",
new XAttribute("name","Email"),
new XAttribute("value","testemail@yahoo.com")),
new XElement("Field",
new XAttribute("name","Invoice"),
new XAttribute("value",x)))))));
int[] invoice = new int[] {1002,1003};
List<XElement> eleList = new List<XElement>();
foreach (int i in invoice)
{
eleList.Add(
new XElement("Invoice",
new XElement("Field",
new XAttribute("name","CustomerNo"),
new XAttribute("value","10000")),
new XElement("Field",
new XAttribute("name","Email"),
new XAttribute("value","testemail@yahoo.com")),
new XElement("Field",
new XAttribute("name","Invoice"),
new XAttribute("value",i)))
);
}
XDocument xDocument = new XDocument(
new XDeclaration("1.0", "UTF-8", null),
new XElement("Request",
new XElement("Operation", "Testing"),
new XElement("Count","2"),
new XElement("Params", eleList)));