0

このコードで C# を使用して xmldocument を生成しました。

  protected XDocument generateXML()
    {
        XDocument xdoc = new XDocument(
 new XDeclaration("1.0", "utf-8", "yes"),
     new XElement("Invoices",
         new XElement("Invoice",
             new XElement("InvoiceNumber", "s10838652")
         .......

 return xdoc;

}

そして別の方法で私が持っている:

    public override void RunWintrackConnector()
    {

        XDocument xml = generateXML();

.....

次に、各 XML ノードにデータを配置します (s10838652 の代わりに (string.Concat(bill.invoice, bill.num);) を InvoiceNumber ノードに割り当てます)。

私は正しい部分を持っていますが、xmlの各ノードにアクセスする方法がわかりません:

xmlnode(for example InvoiceNumber) =  Win2.IntegrationXML.XMLMisc.DirtyData.getStringValue(string.Concat(bill.invoice, bill.num)); 
4

2 に答える 2

0

XElementまたはを使用XDocumentして、必要な値を書き込むことができます。

var valueToInsert = Win2.IntegrationXML.XMLMisc.DirtyData
    .getStringValue(string.Concat(bill.invoice, bill.num));

XDocument xml = generateXML();
xml.Element("Invoices")
    .Elements("Invoice").First() //this First is just an example
    .Element("InvoiceNumber")
    .Value = valueToInsert;

Element()最初に見つかった子ノードを返し、Elements()そのノード名を持つすべての子ノードのリストを取得します。ノードがどのようにネストされているかのパターンを見つけてください。これで準備完了です。

XDocument補足として、変数を使用して上記を実行できますがXElement、追加された Linq 互換性に関してはすべてを保持することをお勧めします。

于 2013-06-11T20:46:48.270 に答える
0
xml
    .Elements("Invoices")
    .Elements("Invoice")
    .Elements("InvoiceNumber")
    .First()
    .Value = string.Concat(bill.invoice, bill.num);

編集 - すべての請求書を確認するには:

foreach(var invoice in xml.Elements("Invoices").Elements("Invoice"))
{
    invoice.Element("InvoiceNumber").Value = "asdf";
}

XPath に精通している場合、これは「Invoices/Invoice」ですべての請求書を選択することと同じです。

于 2013-06-11T20:47:22.413 に答える