[英]linq to xml add element to node
I have this code: 我有以下代码:
XNamespace xsi = XNamespace.Get("http://www.w3.org/2001/XMLSchema-instance");
XNamespace ns = XNamespace.Get("urn:CBI:xsd:CBIBdyPaymentRequest.00.03.09");
XNamespace ns1 = XNamespace.Get("urn:CBI:xsd:CBIPaymentRequest.00.03.09");
XDocument xmlDoc = new XDocument(
new XElement(ns + "CBIBdyPaymentRequest",
new XAttribute(XNamespace.Xmlns + "xsi", xsi.NamespaceName),
new XAttribute(xsi + "schemaLocation", "urn:CBI:xsd:CBIBdyPaymentRequest.00.03.09 CBIBdyPaymentRequest.00.03.09.xsd"),
new XElement(ns +"CBIEnvelPaymentRequest",
new XElement(ns +"CBIPaymentRequest",
new XElement(ns1 + "GrpHdr",
new XElement(ns1 + "MsgId", txtNomeDistinta.Text),
new XElement(ns1 + "CreDtTm", dataCreazioneDistinta.ToString("o")),
new XElement(ns1 + "NbOfTxs", Convert.ToString(listAnagraficheXML.Count)),
new XElement(ns1 + "CtrlSum", getTotalXmlTransactions()),
new XElement(ns1 + "InitgPty",
new XElement(ns1 + "Nm", Properties.Settings.Default.ragioneSociale),
new XElement(ns1 + "Id",
new XElement(ns1 + "OrgId",
new XElement(ns1 + "Othr",
new XElement(ns1 + "Id", Properties.Settings.Default.codiceCuc),
new XElement(ns1 + "Issr", "CBI")
)
)
)
)
), //fine GrpHdr
new XElement(ns1 + "PmtInf",
new XElement(ns1 + "PmtInfId", txtNomeDistinta.Text),
new XElement(ns1 + "PmtMtd", "TRF"),
new XElement(ns1 + "PmtTpInf",
new XElement(ns1 + "InstrPrty", "NORM"),
new XElement(ns1 + "SvcLvl",
new XElement(ns1 + "Cd", "SEPA")
)
),
new XElement(ns1 + "ReqdExctnDt", dataCreazioneDistinta.ToString("yy-MM-dd")),
new XElement(ns1 + "Dbtr",
new XElement(ns1 + "Nm", Properties.Settings.Default.ragioneSociale),
new XElement(ns1 + "Id",
new XElement(ns1 +"OrgId",
new XElement(ns1 + "Othr",
new XElement(ns1 + "Id", Properties.Settings.Default.codiceCuc),
new XElement(ns1 + "Issr", "CBI")
)
)
)
), //fine Dbtr
new XElement(ns1 + "DbtrAcct",
new XElement(ns1 + "Id",
new XElement(ns1 + "IBAN", Properties.Settings.Default.iban)
)
), // fine DbtrAcct
new XElement(ns1 + "DbtrAgt",
new XElement(ns1 + "FinInstnId",
new XElement(ns1 + "ClrSysMmbId",
new XElement(ns1 + "MmbId", Properties.Settings.Default.abi)
)
)
), // fine DbtrAgt
new XElement(ns1 + "ChrgBr", "SLEV")
) //fine PtmInf
) // CBIPaymentRequest
) // fine CBIEnvelPaymentRequest
) //fine CBIBdyPaymentRequest
);
if I save the document it works well but if i want to add element with this code 如果我保存文档,效果很好,但是如果我想用此代码添加元素
var num_trn = 0;
foreach (Anagrafica an in listAnagraficheXML)
{
num_trn++;
XElement el = new XElement(ns1 + "CdtTrfTxInf",
new XElement(ns1 + "PmtId",
new XElement(ns1 + "InstrId", num_trn),
new XElement(ns1 + "EndToEndId", txtNomeDistinta.Text + "-" + num_trn.ToString("D4"))
),
new XElement(ns1 + "PmtTpInf",
new XElement(ns1 + "CtgyPurp",
new XElement(ns1 + "CD", checkForPurpose())
)
),
new XElement(ns1 + "Amt",
new XElement(ns1 + "InstdAmt", new XAttribute("Ccy", "EUR"), getImportXmlTransaction(num_trn))
),
new XElement(ns1 + "Cdtr",
new XElement(ns1 + "Nm", an.nome)
),
new XElement(ns1 + "CdtrAcct",
new XElement(ns1 + "Id",
new XElement(ns1 + "IBAN", an.iban)
)
),
new XElement(ns1 + "RmtInf",
new XElement(ns1 + "Ustrd", getCausalXmlTransaction(num_trn))
)
);
xmlDoc.Element("PtmInf").Add(el);
}
it gives System.NullReferenceException on last line.. I tried with Elements, getName, etc but it gives always same error.. 它在最后一行给出System.NullReferenceException。。我尝试使用Elements,getName等,但始终给出相同的错误。
Why?? 为什么??
Because the Element
method here returns null: 因为这里的
Element
方法返回null:
xmlDoc.Element("PtmInf").Add(el)
You then call Add
on it, so the exception is thrown. 然后,您对其调用
Add
,因此将引发异常。 Element
gets the first children of xmlDoc
(of which there is only ever one) and filters it by element name PtmInf
. Element
获取xmlDoc
的第一PtmInf
元素(其中只有一个),并按元素名称PtmInf
对其进行过滤。 The root element in your document is CBIBdyPaymentRequest
with namespace urn:CBI:xsd:CBIBdyPaymentRequest.00.03.09
, so there is no match. 文档中的根元素是
CBIBdyPaymentRequest
,其名称空间为urn:CBI:xsd:CBIBdyPaymentRequest.00.03.09
,因此没有匹配项。 You've also mistyped PtmInf
instead of PmtInf
. 您还
PtmInf
而不是PmtInf
。
What you probably want is this: 您可能想要的是:
xmlDoc.Descendants(ns1 + "PmtInf").Single().Add(el)
Also you can ignore namespace in searching: 您也可以在搜索中忽略名称空间:
xmlDoc.Elements().Where(e => e.Name.LocalName == "PmtInf"); xmlDoc.Elements()。Where(e => e.Name.LocalName ==“ PmtInf”);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.