我有以下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>
我正在使用以下代码但似乎无法使用来自...的数据创建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"),
};
}
抱歉有大量的XML。我觉得我需要将它显示为我不需要的前几行,并且似乎无法向下导航pp010Rec
如果您能提供任何帮助,我将不胜感激,如果您能指出我可以花时间阅读并单独尝试的方向文献。
干杯,
答案 0 :(得分:2)
您似乎希望值不是属性,因此请更改:
declare @idchar(2)
set @id= ''
select names from Units
where id in(
case @id
when '' then (select id from Units)
else @id
end
)
要:
mgnGrpCod = (string)c.Attribute("mgnGrpCod"),
对其他属性执行相同操作
答案 1 :(得分:2)
我有点改变.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
答案 2 :(得分:1)
你确实需要做JAT在帖子中所说的关于将属性更改为元素的内容,但这并不是你的列表不是从XML创建的原因。这是命名空间(“xmlns =”http://www.123456768.com/technology“),它给你带来麻烦。我不确定你的XML文件试图通过它来实现什么,但是如果你删除它并且做JAT建议您的IEnumerable将开始填充。
有关命名空间的更多信息,您可以从w3查看此链接: http://www.w3schools.com/xml/xml_namespaces.asp