如何使用 ASP.Net C# 在 XmlDocument 中循环创建节点?
How to create node in loop in XmlDocument using ASP.Net C#?
我正在尝试创建 Xml 文件。我想在循环中创建节点
下面是我创建的代码
XDocument doc = new XDocument(
new XElement("Bill",
new XElement("ITEM",
new XElement("Mat",
new XElement("ID","1"),
new XElement("Code"),
new XElement("Name")
),//Mat
new XElement("Store",
new XElement("ID","1"),
new XElement("Code"),
new XElement("Name")
)
)//ITEM
)//Bill
);
doc.save("Test.xml");
输出
<Bill>
<Version>1.0</Version>
<ITEM>
<Mat>
<ID>1<ID/>
<Code />
<Name />
</Mat>
<Store>
<ID>1<ID/>
<Code />
<Name />
</Store>
</ITEM>
</Bill>
我需要像下面这样输出
<Bill>
<Version>1.0</Version>
<ITEM>
<Mat>
<ID>1<ID/>
<Code />
<Name />
</Mat>
<Store>
<ID>1<ID/>
<Code />
<Name />
</Store>
</ITEM>
<ITEM>
<Mat>
<ID>2<ID/>
<Code />
<Name />
</Mat>
<Store>
<ID>2<ID/>
<Code />
<Name />
</Store>
</ITEM>
<ITEM>
<Mat>
<ID>3<ID/>
<Code />
<Name />
</Mat>
<Store>
<ID>3<ID/>
<Code />
<Name />
</Store>
</ITEM>
</Bill>
备注
I will get the value from db and pass it to xml nodes of (ID,Code,Name). I want to iterate in loop..
这是结果图片
您使用的是 xml linq 的 XDocument(不是 xmlDocument)。尝试以下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;
namespace ConsoleApplication11
{
class Program
{
const string FILENAME = @"c:\temp\test.xml";
static void Main(string[] args)
{
string ident = "<?xml version=\"1.0\" encoding=\"utf-8\" ?><BILL></BILL>";
XDocument doc = XDocument.Parse(ident);
XElement bill = doc.Root;
bill.Add(new XElement("version","1.0"));
for (int i = 1; i <= 3; i++)
{
XElement item = new XElement("ITEM");
bill.Add(item);
item.Add(new XElement("Mat", new object[] {
new XElement("ID",i),
new XElement("Code"),
new XElement("Name")
}));
item.Add(new XElement("Store", new object[] {
new XElement("ID",i),
new XElement("Code"),
new XElement("Name")
}));
}
}
}
}
我正在尝试创建 Xml 文件。我想在循环中创建节点
下面是我创建的代码
XDocument doc = new XDocument( new XElement("Bill", new XElement("ITEM", new XElement("Mat", new XElement("ID","1"), new XElement("Code"), new XElement("Name") ),//Mat new XElement("Store", new XElement("ID","1"), new XElement("Code"), new XElement("Name") ) )//ITEM )//Bill ); doc.save("Test.xml");
输出
<Bill>
<Version>1.0</Version>
<ITEM>
<Mat>
<ID>1<ID/>
<Code />
<Name />
</Mat>
<Store>
<ID>1<ID/>
<Code />
<Name />
</Store>
</ITEM>
</Bill>
我需要像下面这样输出
<Bill>
<Version>1.0</Version>
<ITEM>
<Mat>
<ID>1<ID/>
<Code />
<Name />
</Mat>
<Store>
<ID>1<ID/>
<Code />
<Name />
</Store>
</ITEM>
<ITEM>
<Mat>
<ID>2<ID/>
<Code />
<Name />
</Mat>
<Store>
<ID>2<ID/>
<Code />
<Name />
</Store>
</ITEM>
<ITEM>
<Mat>
<ID>3<ID/>
<Code />
<Name />
</Mat>
<Store>
<ID>3<ID/>
<Code />
<Name />
</Store>
</ITEM>
</Bill>
备注
I will get the value from db and pass it to xml nodes of (ID,Code,Name). I want to iterate in loop..
这是结果图片
您使用的是 xml linq 的 XDocument(不是 xmlDocument)。尝试以下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;
namespace ConsoleApplication11
{
class Program
{
const string FILENAME = @"c:\temp\test.xml";
static void Main(string[] args)
{
string ident = "<?xml version=\"1.0\" encoding=\"utf-8\" ?><BILL></BILL>";
XDocument doc = XDocument.Parse(ident);
XElement bill = doc.Root;
bill.Add(new XElement("version","1.0"));
for (int i = 1; i <= 3; i++)
{
XElement item = new XElement("ITEM");
bill.Add(item);
item.Add(new XElement("Mat", new object[] {
new XElement("ID",i),
new XElement("Code"),
new XElement("Name")
}));
item.Add(new XElement("Store", new object[] {
new XElement("ID",i),
new XElement("Code"),
new XElement("Name")
}));
}
}
}
}