嵌套 XML 节点未正确排列
Nested XML node not lining up correctly
我正在尝试制作以下内容 XML
<?xml version="1.0" encoding="utf-8" standalone="no"?>
<Application>
<organisation>
<organisation_id>0</organisation_id>
</organisation>
<address>
<address1>12345</address1>
<address2>qqqqq</address2>
<address3>ddddd</address3>
</address>
<Customer>
<custID>652</custID>
<address1>12345</address1>
<references>
<f_ref>456789</f_ref>
<licenses>
<id>3654</id>
<image>\photo3.jpg</image>
</licenses>
</references>
<type>
<sort>1</sort>
<date>12/12/1997</date>
</type>
<internal>
<auto>true</auto>
<deliver>true</deliver>
</internal>
</Customer>
</Application>
出于这个原因,我有以下代码:
MyDocument MyDoc = new MyDocument(
new XDeclaration("1.0", "utf-8", "no"));
XElement MyRoot = new XElement("Application",
new XElement("organisation",
new XElement("organisation_id", "0")),
new XElement("address",
new XElement("address1", "123456"),
new XElement("address2", "qqqqq"),
new XElement("address3", "ddddd")));
MyDoc.Add(MyRoot);
foreach (var c in GetCustomers())
{
XElement Customers = new XElement("Customer",
new XElement("custID", "652"),
new XElement("address1", "12345")),
new XElement("references",
new XElement("f_ref", "456789")));
MyRoot.Add(Customers);
foreach (License l in c.Licenses)
{
XElement Licenses = new XElement("licenses",
new XElement("id", "3654"),
new XElement("image", "\photo3.jpg"));
MyRoot.Add(Licenses);
}
XElement Type = new XElement("type",
new XElement("sort", "1"),
new XElement("date", "12/12/1997"));
MyRoot.Add(Type);
XElement Internal = new XElement("internal",
new XElement("auto", "true"),
new XElement("deliver", "true"));
MyRoot.Add(Internal);
}
产生以下 XML
<?xml version="1.0" encoding="utf-8" standalone="no"?>
<Application>
<organisation>
<organisation_id>0</organisation_id>
</organisation>
<address>
<address1>12345</address1>
<address2>qqqqq</address2>
<address3>ddddd</address3>
</address>
<Customer>
<custID>652</custID>
<address1>12345</address1>
<references>
<f_ref>456789</f_ref>
</references>
</Customer>
<licenses>
<id>3654</id>
<image>\photo3.jpg</image>
</licenses>
<type>
<sort>1</sort>
<date>12/12/1997</date>
</type>
<internal>
<auto>true</auto>
<deliver>true</deliver>
</internal>
</Application>
我已经移动了 MyRoot.Add
方法,甚至试图在 XElement
中添加 foreach 循环(这给了我一个语法错误)但我不确定如何生成 XML我在之后?
MyRoot
是您的根 Application
元素。这就是您要添加 licences
、type
和 internal
的地方,因此它们将被写入。
您应该将它们添加到 customers
,因此对 type
和 internal
使用 Customers.Add
。 licences
应该包含在 references
构造函数中 - 虽然以类似于 type
和 internal
的方式将 references
的创建拉出可能更容易。
因此,经过一些重新安排以使代码编译:
var doc = new XDocument(
new XDeclaration("1.0", "utf-8", "no"));
var root = new XElement("Application",
new XElement("organisation",
new XElement("organisation_id", "0")),
new XElement("address",
new XElement("address1", "123456"),
new XElement("address2", "qqqqq"),
new XElement("address3", "ddddd")));
doc.Add(root);
var customers = new XElement("Customer",
new XElement("custID", "652"),
new XElement("address1", "12345")
);
root.Add(customers);
var references = new XElement("references",
new XElement("f_ref", "456789"));
var licenses = new XElement("licenses",
new XElement("id", "3654"),
new XElement("image", @"\photo3.jpg"));
references.Add(licenses);
var type = new XElement("type",
new XElement("sort", "1"),
new XElement("date", "12/12/1997"));
customers.Add(type);
var @internal = new XElement("internal",
new XElement("auto", "true"),
new XElement("deliver", "true"));
customers.Add(@internal);
有关工作演示,请参阅 this fiddle。
我正在尝试制作以下内容 XML
<?xml version="1.0" encoding="utf-8" standalone="no"?>
<Application>
<organisation>
<organisation_id>0</organisation_id>
</organisation>
<address>
<address1>12345</address1>
<address2>qqqqq</address2>
<address3>ddddd</address3>
</address>
<Customer>
<custID>652</custID>
<address1>12345</address1>
<references>
<f_ref>456789</f_ref>
<licenses>
<id>3654</id>
<image>\photo3.jpg</image>
</licenses>
</references>
<type>
<sort>1</sort>
<date>12/12/1997</date>
</type>
<internal>
<auto>true</auto>
<deliver>true</deliver>
</internal>
</Customer>
</Application>
出于这个原因,我有以下代码:
MyDocument MyDoc = new MyDocument(
new XDeclaration("1.0", "utf-8", "no"));
XElement MyRoot = new XElement("Application",
new XElement("organisation",
new XElement("organisation_id", "0")),
new XElement("address",
new XElement("address1", "123456"),
new XElement("address2", "qqqqq"),
new XElement("address3", "ddddd")));
MyDoc.Add(MyRoot);
foreach (var c in GetCustomers())
{
XElement Customers = new XElement("Customer",
new XElement("custID", "652"),
new XElement("address1", "12345")),
new XElement("references",
new XElement("f_ref", "456789")));
MyRoot.Add(Customers);
foreach (License l in c.Licenses)
{
XElement Licenses = new XElement("licenses",
new XElement("id", "3654"),
new XElement("image", "\photo3.jpg"));
MyRoot.Add(Licenses);
}
XElement Type = new XElement("type",
new XElement("sort", "1"),
new XElement("date", "12/12/1997"));
MyRoot.Add(Type);
XElement Internal = new XElement("internal",
new XElement("auto", "true"),
new XElement("deliver", "true"));
MyRoot.Add(Internal);
}
产生以下 XML
<?xml version="1.0" encoding="utf-8" standalone="no"?>
<Application>
<organisation>
<organisation_id>0</organisation_id>
</organisation>
<address>
<address1>12345</address1>
<address2>qqqqq</address2>
<address3>ddddd</address3>
</address>
<Customer>
<custID>652</custID>
<address1>12345</address1>
<references>
<f_ref>456789</f_ref>
</references>
</Customer>
<licenses>
<id>3654</id>
<image>\photo3.jpg</image>
</licenses>
<type>
<sort>1</sort>
<date>12/12/1997</date>
</type>
<internal>
<auto>true</auto>
<deliver>true</deliver>
</internal>
</Application>
我已经移动了 MyRoot.Add
方法,甚至试图在 XElement
中添加 foreach 循环(这给了我一个语法错误)但我不确定如何生成 XML我在之后?
MyRoot
是您的根 Application
元素。这就是您要添加 licences
、type
和 internal
的地方,因此它们将被写入。
您应该将它们添加到 customers
,因此对 type
和 internal
使用 Customers.Add
。 licences
应该包含在 references
构造函数中 - 虽然以类似于 type
和 internal
的方式将 references
的创建拉出可能更容易。
因此,经过一些重新安排以使代码编译:
var doc = new XDocument(
new XDeclaration("1.0", "utf-8", "no"));
var root = new XElement("Application",
new XElement("organisation",
new XElement("organisation_id", "0")),
new XElement("address",
new XElement("address1", "123456"),
new XElement("address2", "qqqqq"),
new XElement("address3", "ddddd")));
doc.Add(root);
var customers = new XElement("Customer",
new XElement("custID", "652"),
new XElement("address1", "12345")
);
root.Add(customers);
var references = new XElement("references",
new XElement("f_ref", "456789"));
var licenses = new XElement("licenses",
new XElement("id", "3654"),
new XElement("image", @"\photo3.jpg"));
references.Add(licenses);
var type = new XElement("type",
new XElement("sort", "1"),
new XElement("date", "12/12/1997"));
customers.Add(type);
var @internal = new XElement("internal",
new XElement("auto", "true"),
new XElement("deliver", "true"));
customers.Add(@internal);
有关工作演示,请参阅 this fiddle。