0

我有一个 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 ?

此致。

4

2 回答 2

0

您将需要TRANSACTIONS从 XML 中选择节点。创建子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...");
于 2013-08-28T09:39:06.917 回答
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)));
于 2013-08-28T09:33:03.677 回答