简体   繁体   English

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

[英]Read XML to linq object, then create XML

I have the following XML: 我有以下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 IEnumberable with the data from ... 我正在使用以下代码,但似乎无法使用来自...的数据创建一个IEnumberable。

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. 抱歉,大量的XML。 I felt i needed to display it as the first couple of lines i dont need and cannot seem to navigate down the pp010Rec 我觉得我需要将其显示为我不需要的前几行,并且似乎无法向下浏览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, 干杯,

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 对其他属性执行相同的操作

I a bit changed .xml: 我对.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 更新:链接: http : //rextester.com/UFLPQ70590

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. 您确实需要做JAT在他的帖子中所说的关于将属性更改为元素的内容,但这不是未从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. 这是给您带来麻烦的名称空间(“ xmlns =” http://www.123456768.com/technology“),我不确定您的XML文件试图通过拥有它来完成什么工作,但是如果您删除它并做什么JAT建议您的IEnumerable将开始被填充。

For more information about namespaces, you can check out this link from w3: http://www.w3schools.com/xml/xml_namespaces.asp 有关名称空间的更多信息,您可以从w3中检出此链接: http : //www.w3schools.com/xml/xml_namespaces.asp

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM