0

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..

Why??

xfolder
  • 95
  • 1
  • 10
  • I think because xmlDoc not contains _"PtmInf"_ it contain _ns1+"PmtInf"_ – layonez Jan 12 '17 at 11:31
  • Possible duplicate of [Ignore namespaces in LINQ to XML](http://stackoverflow.com/questions/1145659/ignore-namespaces-in-linq-to-xml) – layonez Jan 12 '17 at 12:10

2 Answers2

2

Because the Element method here returns null:

 xmlDoc.Element("PtmInf").Add(el)

You then call Add on it, so the exception is thrown. Element gets the first children of xmlDoc (of which there is only ever one) and filters it by element name PtmInf. The root element in your document is CBIBdyPaymentRequest with namespace urn:CBI:xsd:CBIBdyPaymentRequest.00.03.09, so there is no match. You've also mistyped PtmInf instead of PmtInf.

What you probably want is this:

 xmlDoc.Descendants(ns1 + "PmtInf").Single().Add(el)
Charles Mager
  • 25,735
  • 2
  • 35
  • 45
  • And also you can get namespace from your xmlDoc `var ns1 = xmlDoc.Root.Name.Namespace;` – layonez Jan 12 '17 at 11:35
  • @LayonezWronskey that would be the wrong namespace in this case. The root and immediate child are in a different namespace to the target element. – Charles Mager Jan 12 '17 at 11:37
  • var ns2 = xmlDoc.Root.Name.Namespace; xmlDoc.Descendants(ns2 + "PtmInf").Single().Add(el); The sequence contains no elements – xfolder Jan 12 '17 at 11:39
  • @MirkoSalvati that's the wrong namespace, as I explained above. You need `ns1` / `urn:CBI:xsd:CBIPaymentRequest.00.03.09`. – Charles Mager Jan 12 '17 at 11:40
  • Some thing like that would work var `ns2 = xmlDoc.Root.Descendants().First().Name.Namespace;` – layonez Jan 12 '17 at 11:41
  • @LayonezWronskey nope, that's still wrong. You need to go about 4 levels down to get the right one. It'd be far easier to just explicitly specify it. – Charles Mager Jan 12 '17 at 11:42
  • Yeap, you right, if that functions in one class you can store namespaces strings globally and then use them in searching – layonez Jan 12 '17 at 11:46
  • not works with var ns2 = xmlDoc.Root.Descendants().First().Name.Namespace; xmlDoc.Descendants(ns2 + "PtmInf").Single().Add(el); and not works with xmlDoc.Element(ns1+"PtmInf").Add(el) or xmlDoc.Descendants(ns1 + "PtmInf").Single().Add(el); – xfolder Jan 12 '17 at 11:49
  • You have a typo. It's `PmtInf`, not `PtmInf`. – Charles Mager Jan 12 '17 at 11:52
0

Also you can ignore namespace in searching:

xmlDoc.Elements().Where(e => e.Name.LocalName == "PmtInf");

layonez
  • 1,746
  • 1
  • 16
  • 20