1

I have this xml file:

<?xml version="1.0" encoding="utf-8" standalone="no"?>
<S2SDDIdf:MPEDDIdfBlkDirDeb xmlns:S2SDDIdf="urn:S2SDDIdf:xsd:$MPEDDIdfBlkDirDeb"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="urn:S2SDDIdf:xsd:$MPEDDIdfBlkDirDeb MPEDDIdfBlkDirDeb.xsd">

  <S2SDDIdf:SndgInst>CHASDEFX</S2SDDIdf:SndgInst>
  <S2SDDIdf:RcvgInst>BOFIIE2D</S2SDDIdf:RcvgInst>
  <S2SDDIdf:pacs.003.001.01 xmlns="urn:iso:std:iso:20022:tech:xsd:pacs.003.001.01">


  </S2SDDIdf:pacs.003.001.01>
</S2SDDIdf:MPEDDIdfBlkDirDeb>

I am adding this element under element S2SDDIdf:pacs.003.001.01:

<DrctDbtTxInf>
    <PmtId>
        <EndToEndId>DDIE2EA00033</EndToEndId>
        <TxId>DDITXA00033</TxId>
    </PmtId>
</DrctDbtTxInf>

Here is the code:

// Read pacs.003.001.01 element
XElement bulk = XElement.Parse(File.ReadAllText("_Bulk.txt"));

// Read DrctDbtTxInf element
XElement tx = XElement.Parse(File.ReadAllText("_Tx.txt"));

// Add DrctDbtTxInf element to pacs.003.001.01 element
bulk.Element("{urn:S2SDDIdf:xsd:$MPEDDIdfBlkDirDeb}pacs.003.001.01").Add(tx);

The problem is that element DrctDbtTxInf gets an empty xmlns attribute:

<DrctDbtTxInf xmlns="">

How do I get rid if it? I tried to supply the same namespace as in pacs.003.001.01 in the DrctDbtTxInf element but then it just stays there which break the app that reads the xml.

1

1 Answer 1

4

Yes, you need to supply the namespace recursivly for all new elements:

public static class Extensions
{
    public static XElement SetNamespaceRecursivly(this XElement root,
                                                  XNamespace ns)
    {
        foreach (XElement e in root.DescendantsAndSelf())
        {
            if (e.Name.Namespace == "")
                e.Name = ns + e.Name.LocalName;
        }

        return root;    
    }
}


XNamespace ns = "urn:iso:std:iso:20022:tech:xsd:pacs.003.001.01";

// Add DrctDbtTxInf element to pacs.003.001.01 element
bulk.Element("{urn:S2SDDIdf:xsd:$MPEDDIdfBlkDirDeb}pacs.003.001.01")
    .Add(tx.SetNamespaceRecursivly(ns));

This will result in the following XML:

<S2SDDIdf:MPEDDIdfBlkDirDeb xmlns:S2SDDIdf="urn:S2SDDIdf:xsd:$MPEDDIdfBlkDirDeb" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="urn:S2SDDIdf:xsd:$MPEDDIdfBlkDirDeb MPEDDIdfBlkDirDeb.xsd">
  <S2SDDIdf:SndgInst>CHASDEFX</S2SDDIdf:SndgInst>
  <S2SDDIdf:RcvgInst>BOFIIE2D</S2SDDIdf:RcvgInst>
  <S2SDDIdf:pacs.003.001.01 xmlns="urn:iso:std:iso:20022:tech:xsd:pacs.003.001.01">
    <DrctDbtTxInf>
      <PmtId>
        <EndToEndId>DDIE2EA00033</EndToEndId>
        <TxId>DDITXA00033</TxId>
      </PmtId>
    </DrctDbtTxInf>
  </S2SDDIdf:pacs.003.001.01>
</S2SDDIdf:MPEDDIdfBlkDirDeb> 
Sign up to request clarification or add additional context in comments.

4 Comments

This is causing issues with the app that received the file. Its currently receives the file without the added 'S2SDDIdf:' and its breaking it.
@Yoav: Sorry, my mistake. I was using the wrong namespace. Please check again.
Thanks, Thats working. I'd give another +1 if I could for introducing to me this 'Extension' pattern.
@Yoav: You are welcome. That "pattern" is called "Extension methods" and is basically just syntactic sugar. But it makes for much more readable code in some cases.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.