将XML读取到linq对象,然后创建XML

ha5z0ras  于 12个月前  发布在  其他
关注(0)|答案(3)|浏览(115)

下面是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>

字符串
我正在使用以下代码,但似乎无法使用来自<pp010Rec>... </pp010Rec>的数据创建IEnumerable

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"),
                };
    }


很抱歉有这么多的XML,我觉得我需要把它显示在我不需要的前几行,而且似乎不能在<pp010Rec>中导航。
如果你能提供任何帮助,我将不胜感激,如果你能指出我的方向文学,我可以花时间阅读,并尝试它独自。
干杯,

w8f9ii69

w8f9ii691#

看起来你想要的是值而不是属性,所以改变:

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

字符串
收件人:

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


对其他属性执行相同的操作

iyfamqjs

iyfamqjs2#

我稍微修改了一下.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>

字符串
所以你可以运行

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);


打印:

CSLN
+222926.00
+0.00
+0.00
+89349.30
+312275.30

CSLM
+55112.00
+0.00
+0.00
+30854.40
+85966.40

**更新:**链接:http://rextester.com/UFLPQ70590

f2uvfpb9

f2uvfpb93#

你确实需要按照JAT在他的帖子中所说的那样将属性更改为元素,但这并不是你的列表不是从XML创建的原因。给你带来麻烦的是命名空间(“xmlns=”http://www.123456768.com/technology“)。我不确定你的XML文件试图通过它来实现什么,但如果你删除它并按照JAT的建议执行,你的IE浏览器将开始填充。
有关名称空间的更多信息,可以从w3:http://www.w3schools.com/xml/xml_namespaces.asp查看此链接

相关问题