Timujin
Timujin

Reputation: 181

Read XML to linq object, then create XML

I have the following XML:

<?xml version="1.0" encoding="UTF-8"?>
<pp010 xmlns="http://www.123456768.com/technology">
    <rptHdr>
        <exchNam>MyXML</exchNam>
        <envText>P</envText>
        <rptCod>pp010</rptCod>
        <rptNam>Daily Stock</rptNam>
        <membLglNam>CompanyA</membLglNam>
        <rptPrntEffDat>2015-04-14</rptPrntEffDat>
        <rptPrntRunDat>2015-04-14</rptPrntRunDat>
    </rptHdr>
    <pp010Grp>
        <pp010KeyGrp>
            <membClgIdCod>HBGKP</membClgIdCod>
        </pp010KeyGrp>
        <pp010Grp1>
            <pp010KeyGrp1>
                <membExchIdCod>JBGJG</membExchIdCod>
            </pp010KeyGrp1>
            <pp010Grp2>
                <pp010KeyGrp2>
                    <currTypCod>CHF</currTypCod>
                </pp010KeyGrp2>
                <pp010Grp3>
                    <pp010KeyGrp3>
                        <acctTypGrp>PP</acctTypGrp>
                    </pp010KeyGrp3>
                    <pp010Rec>
                        <mgnGrpCod>     </mgnGrpCod>
                        <mgnClsCod>CSLN </mgnClsCod>
                        <mgnPremiumAmnt>+222926.00</mgnPremiumAmnt>
                        <mgnLiqDlvAmnt>+0.00</mgnLiqDlvAmnt>
                        <mgnSprdAmnt>+0.00</mgnSprdAmnt>
                        <mgnAddlAmnt>+89349.30</mgnAddlAmnt>
                        <unadjMgnReq>+312275.30</unadjMgnReq>
                    </pp010Rec>
                    <pp010Rec>
                        <mgnGrpCod>     </mgnGrpCod>
                        <mgnClsCod>CSLM </mgnClsCod>
                        <mgnPremiumAmnt>+55112.00</mgnPremiumAmnt>
                        <mgnLiqDlvAmnt>+0.00</mgnLiqDlvAmnt>
                        <mgnSprdAmnt>+0.00</mgnSprdAmnt>
                        <mgnAddlAmnt>+30854.40</mgnAddlAmnt>
                        <unadjMgnReq>+85966.40</unadjMgnReq>
                    </pp010Rec>

I am using the following code but cannot seem to create an IEnumerable with the data from <pp010Rec>... </pp010Rec>

public class MarginRep
{
    public string mgnGrpCod { get; set; }
    public string mgnClsCod { get; set; }
    public string mgnPremiumAmnt { get; set; }
    public string mgnLiqDlvAmnt { get; set; }
    public string mgnSprdAmnt { get; set; }
    public string mgnAddlAmnt { get; set; }
    public string unadjMgnReq { get; set; }
}

 private void button1_Click(object sender, EventArgs e)
 {
     string file =    @"D:\WorkDesktop\VisualStudio\file.xml";

     XDocument xmlDoc = XDocument.Load(file);

     IEnumerable<MarginRep> myMarginRep = 
         from c in xmlDoc.Descendants("pp010Rec")
         select new MarginRep()
                {
                    mgnGrpCod = (string)c.Attribute("mgnGrpCod"),
                    mgnClsCod = (string)c.Attribute("mgnClsCod"),
                    mgnPremiumAmnt = (string)c.Attribute("mgnPremiumAmnt"),
                    mgnLiqDlvAmnt = (string)c.Attribute("mgnLiqDlvAmnt"),
                    mgnSprdAmnt = (string)c.Attribute("mgnSprdAmnt"),
                    mgnAddlAmnt = (string)c.Attribute("mgnAddlAmnt"),
                    unadjMgnReq = (string)c.Attribute("unadjMgnReq"),
                };
    }

Sorry for the large amount of the XML. I felt I needed to display it as the first couple of lines I don't need and cannot seem to navigate down the <pp010Rec>.

IF you can offer any help I would be grateful, if you can point me into the direction literature I can spend time reading and try it alone.

Cheers,

Upvotes: 0

Views: 112

Answers (3)

FoggyFinder
FoggyFinder

Reputation: 2220

I a bit changed .xml:

<?xml version="1.0" encoding="UTF-8"?>
<pp010 xmlns="http://www.123456768.com/technology">
<rptHdr>
<exchNam>MyXML</exchNam>
<envText>P</envText>
<rptCod>pp010</rptCod>
<rptNam>Daily Stock</rptNam>
<membLglNam>CompanyA</membLglNam>
<rptPrntEffDat>2015-04-14</rptPrntEffDat>
<rptPrntRunDat>2015-04-14</rptPrntRunDat>
</rptHdr>
<pp010Grp>
<pp0510KeyGrp>
<membClgIdCod>HBGKP</membClgIdCod>
</pp0510KeyGrp>
<pp010Grp1>
<pp010KeyGrp1>
<membExchIdCod>JBGJG</membExchIdCod>
</pp010KeyGrp1>
<pp010Grp2>
<pp010KeyGrp2>
<currTypCod>CHF</currTypCod>
</pp010KeyGrp2>
<pp010Grp3>
<pp010KeyGrp3>
<acctTypGrp>PP</acctTypGrp>
</pp010KeyGrp3>
<pp010Rec>
    <mgnGrpCod>     </mgnGrpCod>
    <mgnClsCod>CSLN </mgnClsCod>
    <mgnPremiumAmnt>+222926.00</mgnPremiumAmnt>
    <mgnLiqDlvAmnt>+0.00</mgnLiqDlvAmnt>
    <mgnSprdAmnt>+0.00</mgnSprdAmnt>
    <mgnAddlAmnt>+89349.30</mgnAddlAmnt>
    <unadjMgnReq>+312275.30</unadjMgnReq>
</pp010Rec>
<pp010Rec>
    <mgnGrpCod>     </mgnGrpCod>
    <mgnClsCod>CSLM </mgnClsCod>
    <mgnPremiumAmnt>+55112.00</mgnPremiumAmnt>
    <mgnLiqDlvAmnt>+0.00</mgnLiqDlvAmnt>
    <mgnSprdAmnt>+0.00</mgnSprdAmnt>
    <mgnAddlAmnt>+30854.40</mgnAddlAmnt>
    <unadjMgnReq>+85966.40</unadjMgnReq>
</pp010Rec>

</pp010Grp3>
</pp010Grp2>
</pp010Grp1>
</pp010Grp>

</pp010>

So you can run

public class MarginRep
    {
        public string mgnGrpCod { get; set; }
        public string mgnClsCod { get; set; }
        public string mgnPremiumAmnt { get; set; }
        public string mgnLiqDlvAmnt { get; set; }
        public string mgnSprdAmnt { get; set; }
        public string mgnAddlAmnt { get; set; }
        public string unadjMgnReq { get; set; }

        public override string ToString()
        {
            return string.Format("{0}\n{1}\n{2}\n{3}\n{4}\n{5}\n{6}",
             mgnGrpCod, mgnClsCod, mgnPremiumAmnt, mgnLiqDlvAmnt, mgnSprdAmnt,
             mgnAddlAmnt, unadjMgnReq);
        }
    }

    var xmlDoc = XDocument.Load("1.xml");
    XNamespace xn = xmlDoc.Root.Name.Namespace;
    IEnumerable<MarginRep> myMarginRep = xmlDoc.Root.Descendants(xn + "pp010Rec")
    .Select(c => new MarginRep()
        {
            mgnGrpCod = c.Element(xn + "mgnGrpCod").Value,
            mgnClsCod = c.Element(xn + "mgnClsCod").Value,
            mgnPremiumAmnt = c.Element(xn + "mgnPremiumAmnt").Value,
            mgnLiqDlvAmnt = c.Element(xn + "mgnLiqDlvAmnt").Value,
            mgnSprdAmnt = c.Element(xn + "mgnSprdAmnt").Value,
            mgnAddlAmnt = c.Element(xn + "mgnAddlAmnt").Value,
            unadjMgnReq = c.Element(xn + "unadjMgnReq").Value
        });
    foreach (var x in myMarginRep)
        Console.WriteLine(x);

Print:

CSLN
+222926.00
+0.00
+0.00
+89349.30
+312275.30

CSLM
+55112.00
+0.00
+0.00
+30854.40
+85966.40

Update: Link: http://rextester.com/UFLPQ70590

Upvotes: 2

user1011627
user1011627

Reputation: 1831

You do need to do what JAT said in his post about changing the attributes to elements, but that isn't the reason why your list is not being created from the XML. It's the namespace ("xmlns="http://www.123456768.com/technology") that is giving you trouble. I'm not sure what your XML file is trying to accomplish by having it, but if you remove it and do what JAT recommended your IEnumerable will start to be populated.

For more information about namespaces, you can check out this link from w3: http://www.w3schools.com/xml/xml_namespaces.asp

Upvotes: 1

The One
The One

Reputation: 4696

It seems you want the values not the attributes, so change:

  mgnGrpCod = (string)c.Attribute("mgnGrpCod"),

To:

  mgnGrpCod = (string)c.Element("mgnGrpCod").Value,

Do the same with the other properties

Upvotes: 2

Related Questions