我需要将一些特定的子节点加载到文本框,但它们被构造为父节点内的子节点。
XML结构:
<nfeProc versao="3.10" xmlns="http://www.portalfiscal.inf.br/nfe">
<NFe xmlns="http://www.portalfiscal.inf.br/nfe">
<det nItem="1">
<prod>
<cProd>09</cProd>
<cEAN />
<xProd>CHAPEU DE PALHA SOBRAL</xProd>
</prod>
<imposto>
<vTotTrib>30.23</vTotTrib>
<ICMS>
<ICMSSN101>
<orig>0</orig>
<CSOSN>101</CSOSN>
<pCredSN>2.5600</pCredSN>
<vCredICMSSN>2.46</vCredICMSSN>
</ICMSSN101>
</ICMS>
</imposto>
</det>
</protNFe>
</nfeProc>
在XML中,有各种节点。对于每一个,我需要加载一些数据。到目前为止我的代码是:
XmlNodeList xndet = xml.GetElementsByTagName("det");
foreach (XmlNode xn in xndet)
{
string cProd = xn.SelectSingleNode("/prod/cProd").InnerText.ToString();
txCPROD.Text = cProd;
string vCredICMSSN = xn.SelectSingleNode("/imposto/ICMS/ICMSSN101/vCredICMSSN").InnerText.ToString();
txICMSSN.Text = vCredICMSSN;
// do other things...
}
此处发生错误:
"string cProd = xn.SelectSingleNode("/prod/cProd").InnerText.ToString();
我的逻辑说,在xn节点内,字符串将接收cProd元素的内部文本。
但是我得到一个未处理的类型&#39; System.NullReferenceException&#39;发生了
答案 0 :(得分:1)
您的XML无效,<html>
<head>
<title></title>
<!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css">
<!-- jQuery library -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<!-- Latest compiled JavaScript -->
<script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
<style type="text/css">
.main{
background-image: url('http://www.detaildesignonline.com/contentimages/sky1285264350.jpg');
min-height: 600px;
border-bottom-style: solid;
border-bottom-color: rgba(4,58,106,1);
border-bottom-width: 10px;
}
.heading{
font-weight: 900;
color:#004B81;
}
.more{
color:rgba(159,67,66,1);
}
.leftForm{
background-color: rgba(224,237,222,1);
height: 350px;
}
.rightForm{
background-color: rgba(253,253,253,0.7);
height: 350px;
}
</style>
</head>
<body>
<div style="height:10px; background-color:orange"></div>
<div class="row">
<div class="col-sm-1">
</div>
<div class="col-sm-10">
<div><img src="http://tollfreecustomercarenumber.com/wp-content/uploads/2015/11/ICICI-bank.jpg" height="100px" width="250px"></div>
<div class=""></div>
</div>
<div class="col-sm-1"></div>
</div>
<div class="row">
<div class="col-sm-1">
</div>
<div class="col-sm-10 main">
<div class="col-sm-1">
</div>
<div class="col-sm-11">
<h3 class="heading">We've got all your Forex needs covered!</h3>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Maecenas vitae mi et leo tristique <br>tincidunt ac nec est. Aenean volutpat mauris vitae erat ultricies varius.</p>
<p><b class="more">What's more? </b><b>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Maecenas <br>vitae mi et leo tristique tincidunt ac nec est.</b></p>
</div>
<div class="col-sm-1"></div>
<div class="col-sm-5 leftForm">
<form role="form">
<br>
<h3 style="color:rgba(159,67,66,1); margin-top:0px">Sign In</h3>
<div class="form-group col-xs-8">
E-Mail ID
<input type="email" style="border: 2px solid orange;" class="form-control" id="email">
</div>
<div class="form-group col-xs-8">
Password
<input type="password" style="border: 2px solid orange;" class="form-control" id="pwd">
</div>
<div class="col-xs-12">
<input type="submit" class="btn btn-default" style="background-color:rgba(159,67,66,1); color:white" value="LOG IN"></input><br>
<small style="color:rgba(159,67,66,1)"><u><a style="color:rgba(159,67,66,1)" href="#">Forgot Password?</a></u></small>
</div>
</form>
</div>
<div class="col-sm-5 rightForm">
<form role="form">
<br>
<h3 style="margin-top:0px">Sign Up</h3>
<div class="form-group col-xs-8">
*E-Mail ID
<input type="email" class="form-control" id="email">
</div>
<div class="form-group col-xs-8">
*Password
<input type="password" class="form-control" id="pwd">
</div>
<div class="form-group col-xs-8">
*Re-enter Password
<input type="password" class="form-control" id="pwd">
</div>
<div class=" col-xs-12">
<button type="submit" class="btn btn-default" style="background-color:rgba(102,102,102,1); color:white">SIGN UP</button>
</div>
</form>
</div>
<div class="col-sm-1"></div>
<!-- <p style="height:10px; bottom:0px; background-color:rgba(4,58,106,1)"></p> -->
</div>
<div class="col-sm-1"></div>
</div>
<div class="row">
<div class="col-sm-1">
</div>
<div class="col-sm-10">
<br>
<div style="line-height:23px"><p style="color:rgba(102,102,102,1); font-size:12px">Lorem ipsum dolor sit amet, consectetur adipiscing elit. Maecenas vitae mi et leo tristique tincidunt ac nec est. Aenean volutpat mauris vitae erat ultricies varius. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Maecenas vitae mi et leo tristique tincidunt ac nec est. Aenean volutpat mauris vitae erat ultricies varius. Aenean volutpat mauris vitae erat ultricies varius. Lorem ipsum dolor sit amet</p></div>
<div class=""></div>
</div>
<div class="col-sm-1"></div>
</div>
的结束标记为<NFe>
,因此我认为您已复制/粘贴错误,否则您的代码根本无法运行。
我比</protNFe>
类更喜欢System.Xml.Linq.XDocument
,我发现使用它更容易。
以下是获取System.Xml
元素中的值列表的解决方案。在这里,我通过使用元素的cProd
属性来忽略命名空间。只有在您确定不需要打扰命名空间时才这样做。根据您的示例xml,我认为您不需要,因为没有其他元素具有相同的名称,但在我可以看到的不同命名空间中。
Name.LocalName
如果您想使用命名空间安全地玩它,您可以这样做:
string theXML = @"<nfeProc versao=""3.10"" xmlns=""http://www.portalfiscal.inf.br/nfe""><NFe xmlns=""http://www.portalfiscal.inf.br/nfe""><det nItem=""1""><prod><cProd>09</cProd><cEAN /><xProd>CHAPEU DE PALHA SOBRAL</xProd></prod><imposto><vTotTrib>30.23</vTotTrib><ICMS><ICMSSN101><orig>0</orig><CSOSN>101</CSOSN><pCredSN>2.5600</pCredSN><vCredICMSSN>2.46</vCredICMSSN></ICMSSN101></ICMS></imposto></det><det nItem=""2""><prod><cProd>10</cProd><cEAN /><xProd>CHAPEU DE PALHA SOBRAL</xProd></prod><imposto><vTotTrib>30.23</vTotTrib><ICMS><ICMSSN101><orig>0</orig><CSOSN>101</CSOSN><pCredSN>2.5600</pCredSN><vCredICMSSN>2.46</vCredICMSSN></ICMSSN101></ICMS></imposto></det></NFe></nfeProc>";
XDocument doc = XDocument.Parse(theXML);
var cProdNodes = doc.Descendants().Elements().Where (d => d.Name.LocalName == "cProd");
List<string> cProdValues = cProdNodes.Select (pn => pn.Value).ToList();