告诉所有人,我试图将当前的XML文件重写为一个新的XML文件而没有那些不再列出待售的项目但不幸的是我似乎错过了一些东西
这是原始XML文件
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<imgdir name="Commodity.img">
<imgdir name="0">
<int name="SN" value="10000000"/>
<int name="ItemId" value="1002000"/>
<int name="Count" value="1"/>
<int name="Price" value="300"/>
<int name="Period" value="14"/>
<int name="Priority" value="8"/>
<int name="Gender" value="2"/>
<int name="OnSale" value="0"/>
</imgdir>
<imgdir name="1">
<int name="SN" value="10000001"/>
<int name="ItemId" value="1002015"/>
<int name="Count" value="1"/>
<int name="Price" value="300"/>
<int name="Period" value="14"/>
<int name="Priority" value="8"/>
<int name="Gender" value="2"/>
<int name="OnSale" value="0"/>
</imgdir>
<imgdir name="2">
<int name="SN" value="10000002"/>
<int name="ItemId" value="1002187"/>
<int name="Count" value="1"/>
<int name="Price" value="300"/>
<int name="Period" value="14"/>
<int name="Priority" value="8"/>
<int name="Gender" value="2"/>
<int name="OnSale" value="0"/>
</imgdir>
<imgdir name="3">
<int name="SN" value="10000003"/>
<int name="ItemId" value="1002292"/>
<int name="Count" value="1"/>
<int name="Price" value="300"/>
<int name="Period" value="14"/>
<int name="Priority" value="8"/>
<int name="Gender" value="2"/>
<int name="OnSale" value="0"/>
</imgdir>
这是我从代码中获得的输出
<?xml version="1.0" encoding="utf-8"?><imgdir xmlns="Commodity.img">
<imgdir SN="10002304" ItemId="5010073" Count="1" Price="3300" Period="90" Priority="12" Gender="2" OnSale="1" xmlns="2" />
<imgdir SN="10002305" ItemId="5010074" Count="1" Price="3300" Period="90" Priority="12" Gender="2" OnSale="1" xmlns="3" />
<imgdir SN="10002306" ItemId="1022104" Count="1" Price="3300" Period="90" Priority="12" Gender="2" OnSale="1" xmlns="4" />
<imgdir SN="10002307" ItemId="9102232" Count="1" Price="16900" Period="90" Priority="12" Gender="2" OnSale="1" xmlns="5" />
<imgdir SN="10002308" ItemId="1003050" Count="1" Price="2500" Period="90" Priority="12" Gender="2" OnSale="1" xmlns="6" />
<imgdir SN="10002309" ItemId="1022102" Count="1" Price="3200" Period="90" Priority="12" Gender="2" OnSale="1" xmlns="7" />
</imgdir>
任何建议我做错了什么?
XmlWriter writer = XmlWriter.Create("NewXml.xml");
int number = 0;
string OLD_itemSN = "0";
writer.WriteStartElement("imgdir", "Commodity.img");
foreach (Cashitem item in Cashitems)
{
if (item.OnSale != 0)
{
if (item.SN.ToString() != OLD_itemSN)
{
number++;
OLD_itemSN = item.SN.ToString();
}
writer.WriteStartElement("imgdir", number.ToString());
writer.WriteAttributeString("SN", item.SN.ToString());
writer.WriteAttributeString("ItemId", item.ItemId.ToString());
writer.WriteAttributeString("Count", item.Count.ToString());
writer.WriteAttributeString("Price", item.Price.ToString());
writer.WriteAttributeString("Period", item.Period.ToString());
writer.WriteAttributeString("Priority", item.Priority.ToString());
writer.WriteAttributeString("Gender", item.Gender.ToString());
writer.WriteAttributeString("OnSale", item.OnSale.ToString());
if (item.Limit != null)
{
writer.WriteAttributeString("Limit", item.Limit);
}
writer.WriteEndElement();
}
}
writer.WriteEndElement();
writer.Flush();
}
在我的输出中我想念imgdir Name = Value 和每个属性之前的int 我真的很绝望:(
答案 0 :(得分:0)
请勿混淆属性和元素!
试试这段代码:
var settings = new XmlWriterSettings { Indent = true };
using (XmlWriter writer = XmlWriter.Create("NewXml.xml", settings))
{
writer.WriteStartDocument(standalone: true);
int number = 0;
string OLD_itemSN = "0";
writer.WriteStartElement("imgdir");
writer.WriteAttributeString("name", "Commodity.img");
foreach (Cashitem item in Cashitems)
{
if (item.OnSale != 0)
{
if (item.SN.ToString() != OLD_itemSN)
{
number++;
OLD_itemSN = item.SN.ToString();
}
writer.WriteStartElement("imgdir");
writer.WriteAttributeString("name", number.ToString());
WriteIntNode(writer, "SN", item.SN.ToString());
WriteIntNode(writer, "ItemId", item.ItemId.ToString());
WriteIntNode(writer, "Count", item.Count.ToString());
WriteIntNode(writer, "Price", item.Price.ToString());
WriteIntNode(writer, "Period", item.Period.ToString());
WriteIntNode(writer, "Priority", item.Priority.ToString());
WriteIntNode(writer, "Gender", item.Gender.ToString());
WriteIntNode(writer, "OnSale", item.OnSale.ToString());
if (item.Limit != null)
{
writer.WriteAttributeString("Limit", item.Limit);
}
writer.WriteEndElement();
}
}
}
另一种方法:
void WriteIntNode(XmlWriter writer, string name, string value)
{
writer.WriteStartElement("int");
writer.WriteAttributeString("name", name);
writer.WriteAttributeString("value", value);
writer.WriteEndElement();
}