我有一个xml,但我不知道我怎么写这个。因为这个xml里面有子列表。我不知道怎么写呢?你能帮我解决这个问题吗?
<?xml version="1.0" encoding="ISO-8859-9"?>
<SALES_INVOICES>
<INVOICE DBOP="INS" >
<TYPE>9</TYPE>
<TRANSACTIONS>
<TRANSACTION>
<TYPE>4</TYPE>
</TRANSACTION>
<TRANSACTION>
<TYPE>4</TYPE>
</TRANSACTION>
</TRANSACTIONS>
<PAYMENT_LIST>
<PAYMENT>
<DATE>01.01.2013</DATE>
</PAYMENT>
</PAYMENT_LIST>
<AFFECT_RISK>0</AFFECT_RISK>
<DOC_DATE>19.03.2013</DOC_DATE>
<GUID></GUID>
<EDURATION_TYPE>0</EDURATION_TYPE>
<EDTCURR_GLOBAL_CODE>TL</EDTCURR_GLOBAL_CODE>
</INVOICE>
</SALES_INVOICES>
我写代码;
using (XmlWriter writer = XmlWriter.Create("C:\\1.xml"))
{
writer.WriteStartDocument();
writer.WriteStartElement("SALES_INVOICES");
writer.WriteStartElement("INVOICE");
writer.WriteAttributeString("DBOP", "INS");
writer.WriteStartElement("Trancastions");
foreach (var transaction in orderTransactionDto)
{
writer.WriteStartElement("Trancastion");
writer.WriteElementString("ID", transaction.Id.ToString());
writer.WriteEndElement();
}
writer.WriteEndElement();
writer.WriteEndElement();
writer.WriteEndDocument();
}
如何将此linq写入xml?
最诚挚的问候。
答案 0 :(得分:0)
不是那么难,试试这个:
var TRANSACTIONS=new XElement("TRANSACTIONS");
TRANSACTIONS.Add(new XElement("TRANSACTION",new XElement("TYPE",4)));
TRANSACTIONS.Add(new XElement("TRANSACTION",new XElement("TYPE",5)));
TRANSACTIONS.Add(new XElement("TRANSACTION",new XElement("TYPE",6)));
答案 1 :(得分:0)
您需要从XML中选择TRANSACTIONS
节点。创建子TRANSACTION
节点并设置子TYPE
值。您可以使用LINQ to XML执行此操作:
//Load the doc
XDocument xml = XDocument.Load(HttpContext.Current.Server.MapPath("your xml file..."));
//Get the transactions node
var transactionsNode = xml.Descendants().Elements("TRANSACTIONS").SingleOrDefault();
//Create a child transaction node
XElement elem = new XElement("TRANSACTION");
//Create a child type node and set its value to 200
elem.SetElementValue("TYPE", 200);
//Append this to the parent transactions node
transactionsNode.Add(elem);
//Save the XML back to file
xml.Save(HttpContext.Current.Server.MapPath("your xml file...");