嗨伙计们有谁知道我怎么做,让我的Xmlnodelist在Foreach中。 [英] Hi Guys Does Anyone Know How I Can, Get My Xmlnodelist Inside A Foreach.
问题描述
您好我有问题,我想在我的xmlnode foreach中得到我的xmlnodelist,因为我有反击它,我需要得到addon属性匹配文本框和节点的id 。
我的最终目标是一个篮子。我计算文本框中的数字并计算,它的价格来自一个名为AddonPrice的xmlnode。
现在我只得到第一个插件而不是其他两个,所以它现在看起来像这样。
类型:票数: 1价格:178总价:178
和计数工作正常,但只有第一个节点。我想我的问题是这行doc.SelectNodes(TourInfo / AddOns / AddOn [@ Id ='+ id +']);.因为它在我的foreach之外所以id计数总是为0.所以我需要帮助才能把它放到我的foreach里面,或者用其他方式做吧:)
< pre lang =c#> string id = addon + count;
string addon;
XmlNodeList nodeListADDon = doc.SelectNodes( TourInfo / AddOns / AddOn [@ Id =' + id + ']);
litTotal.Text = ;
litAdd.Text = ;
foreach (XmlNode nodeA in nodeListADDon)
{
addon = Request.Form [ txtAddonAntal + count];
antal = int .Parse(addon.ToString());
pris = int .Parse(nodeA [ AddonPrice跨度>]的innerText)。
ansver + = antal * pris;
总计+ = ansver;
litTotal.Text + = total.ToString()+ & nbsp; kr 跨度>;
litAdd.Text = < table class = table table-condensed >< tr> + < span class =code-string>< td> + nodeA [ AddonText]。InnerText + < / td> + < td> + antal.ToString()+ < / td> + < td> Pris: & nbsp; + ansver.ToString()+ & nbsp; kr< / td> + < / tr>< / table>< br />跨度>;
count ++;
}
编辑
<? xml < span class =code-summarycomment> version
= 1.0 standalone = 没有 ? >
<! - 0031114_reply.xml - 由ACUCOBOL-GT v9.2.3 + ECN-4310 + ECN-生成2015/06/25上的4314 + ECN-4316 - >
< TourInfo >
< 记录类型 > TourInfo < / Recordtype >
< 旅行 > 1609 < / Trip >
< TransportForm > 2 < / TransportForm >
< 住宿 > 200 < / Accommodation >
< AvailableTrips > 14 < / AvailableTrips >
< AddOns >
< AddOn Id = < span class =code-keyword> addon0 >
< AddonCode > 1 < < span class =code-leadattribute> / AddonCode >
< AddonText > Barnestol < / AddonText >
< AddonAccNo > 200 < / AddonAccNo >
< AddonPrice > 150 < / AddonPrice >
< AddonPriceOffersText > Tilbud:< span class =code-keyword>< / AddonPriceOffersText >
< AddonPriceOffers > 125 < / AddonPriceOffers >
< / AddOn >
< AddOn Id = addon1 >
< AddonCode > 2 < / AddonC ode >
< AddonText > Tremmeseng < / AddonText >
< AddonAccNo > 200 < / AddonAccNo >
< AddonPrice > 200 < / AddonPrice >
< AddonPriceOffersText / >
< AddonPriceOffers / >
< / AddOn < span class =code-keyword>>
< AddOn Id = addon2 >
< AddonCode > 9 < / AddonCode >
< AddonText > 方坯< / AddonText >
< AddonAccNo > 67 < / AddonAccNo >
< AddonPrice > 178 < / AddonPrice >
< AddonPriceOffersText / >
< AddonPriceOffers / >
< / AddOn >
< < span class =code-leadattribute> / AddOns >
好的 - 我明白了:
你是对的。列表节点列表仅针对1个id迭代一次。尝试将其包装在中
循环:
如果正在迭代id
for ( int x = 0 ; x< doc.SelectNodes( TourInfo / AddOns / AddOn)。计数; x ++)
{
string addon ;
XmlNodeList nodeListADDon = doc.SelectNodes( TourInfo / AddOns / AddOn [@ Id ='插件 + x + ']);
litTotal.Text = ;
litAdd.Text = ;
foreach (XmlNode nodeA in nodeListADDon)
{
addon = Request.Form [ txtAddonAntal + x];
antal = int .Parse(addon.ToString());
pris = int .Parse(nodeA [ AddonPrice跨度>]的innerText)。
ansver + = antal * pris;
总计+ = ansver;
// 为清晰起见而格式化
litTotal.Text + = total.ToString()+ & nbsp; kr;
string text = < table class =表格精简 >< tr>
text + = < td> + nodeA [ AddonText]。InnerText + < / td>
text + = < td> + antal.ToString()+ < / td>
text + = < td> Pris:& nbsp; + ansver.ToString()
text + = & nbsp; kr< / td>
text + = < / tr>< / table>< br />< /跨度>;
litAdd.Text = text;
}
}
更新:我现在可以访问OP源代码:
private static void testsss()
{
XmlDocument doc = new XmlDocument();
doc.Load( @ C:\ nrd Party\testsss.xml );
int total = 0 ;
litTotal.Text = ;
litAdd.Text = ;
// 错误处理
var allAddonNodes = doc.SelectNodes( TourInfo / AddOns / AddOn );
if (allAddonNodes == null )
返回跨度>;
for ( int x = 0 ; x < allAddonNodes.Count; x ++)
{
string textBoxname = string .Format( txtAddonAntal {0},x);
XmlNode addonNode = allAddonNodes [x];
// 错误处理
if (addonNode [ AddonPrice] == null )
继续;
// 错误处理
int antal;
if (!int.TryParse(Request.Form [textBoxname], out antal))
继续;
// 错误处理
int pris;
if (!int.TryParse(addonNode [ AddonPrice]。InnerText, out pris))
continue ;
var ansver = antal * pris;
总计+ = ansver;
var nodeAAddonText = addonNode [ AddonText跨度>];
string nodeAInnerText = nodeAAddonText == null ? :nodeAAddonText.InnerText;
// 此行似乎不合适
litAdd。 Text = string .Format(FormatText,nodeAInnerText,antal,ansver);
}
litTotal.Text =总计+ @ & nbsp; kr 跨度>;
}
最终更新:有关一些想法litAdd:
private static void testsss()
{
//如果你有一个表包含在标记中然后在服务器
//运行它并改为使用它。下面的方法有点像黑客。
//我们将使用它将表标记写入字符串。这是hack部分
StringBuilder sb = new StringBuilder();
HtmlTextWriter writer = new HtmlTextWriter(new StringWriter(sb,CultureInfo.InvariantCulture));
//在hack中使用的表
HtmlTable table = new HtmlTable();
table.Attributes.Add(class,table table-condensed);
XmlDocument doc = new XmlDocument();
doc.Load(@C:\ 3rd Party\testsss.xml);
int total = 0;
litTotal.Text =;
litAdd.Text =;
//错误处理
var allAddonNodes = doc.SelectNodes(TourInfo / AddOns / AddOn);
if(allAddonNodes == null)
return;
for(int x = 0; x< allAddonNodes.Count; x ++)
{
//创建一个新表行
HtmlTableRow row = new HtmlTableRow( );
//要添加到行的单元格实例。
HtmlTableCell单元格;
string textBoxname = string.Format(txtAddonAntal {0},x);
XmlNode addonNode = allAddonNodes [x];
//错误处理
if(addonNode [AddonPrice] == null)
continue;
//错误处理
int antal;
if(!int.TryParse(RequestForm [textBoxname],out anntal))
continue;
//错误处理
int pris;
if(!int.TryParse(addonNode [AddonPrice]。InnerText,out pris))
继续;
var ansver = antal * pris;
总计+ = ansver;
var nodeAAddonText = addonNode [AddonText];
string nodeAInnerText = nodeAAddonText == null? :nodeAAddonText.InnerText;
cell = new HtmlTableCell();
cell.InnerText = nodeAInnerText;
row.Cells.Add(cell);
cell = new HtmlTableCell();
cell.InnerText = antal.ToString();
row.Cells.Add(cell);
cell = new HtmlTableCell();
cell.InnerText = string.Format(Pris:& nbsp; {0}& nbsp; kr,ansver);
row.Cells.Add(cell);
//将行添加到表
table.Rows.Add(row);
}
//如果表已经存在,那么页面渲染将绘制它,行和单元格
//在呈现页面时。在这个hack中,它不在页面中,所以我们必须自己渲染
//并获取html。这是一个黑客(但很好;)
table.RenderControl(writer);
litAdd.Text = sb.ToString();
litTotal.Text = total + @& nbsp; kr;
}
Hi i have a problem, i want to get my xmlnodelist inside my xmlnode foreach somehowe because i have counter on it, an i need to get the of addon attribute match the id of textbox and nodes.
My final goal is a shooping basket. Where i count the number in the textbox and counting, it with the price that comes from an xmlnode called AddonPrice.
Right now i only get the first addon not the two others, so it looks like this right now.
Type:Ticket counts:1 price:178 total price: 178
and the counting works fine, but only with the first node. I think my problem is this line doc.SelectNodes("TourInfo/AddOns/AddOn[@Id='" + id + "']");. Because its outside my foreach so the id count will allways be 0. So i need help to get it inside my foreach or, do it some other way :)
string id = "addon" + count;
string addon;
XmlNodeList nodeListADDon = doc.SelectNodes("TourInfo/AddOns/AddOn[@Id='" + id + "']");
litTotal.Text = "";
litAdd.Text = "";
foreach (XmlNode nodeA in nodeListADDon)
{
addon = Request.Form["txtAddonAntal" + count];
antal = int.Parse(addon.ToString());
pris = int.Parse(nodeA["AddonPrice"].InnerText);
ansver += antal * pris;
total += ansver;
litTotal.Text += total.ToString() + " kr";
litAdd.Text = "<table class="table table-condensed"><tr>" + "<td>" + nodeA["AddonText"].InnerText + "</td>" + "<td>" + antal.ToString() + "</td>" + "<td>Pris: " + ansver.ToString() + " kr</td>" + "</tr></table><br />";
count++;
}
EDIT
<?xml version="1.0" standalone="no"?>
<!-- 0031114_reply.xml - generated by ACUCOBOL-GT v9.2.3 + ECN-4310 + ECN-4314 + ECN-4316 on 2015/06/25 -->
<TourInfo>
<Recordtype>TourInfo</Recordtype>
<Trip>1609</Trip>
<TransportForm>2</TransportForm>
<Accommodation>200</Accommodation>
<AvailableTrips>14</AvailableTrips>
<AddOns>
<AddOn Id="addon0">
<AddonCode>1</AddonCode>
<AddonText>Barnestol</AddonText>
<AddonAccNo>200</AddonAccNo>
<AddonPrice>150</AddonPrice>
<AddonPriceOffersText>Tilbud:</AddonPriceOffersText>
<AddonPriceOffers>125</AddonPriceOffers>
</AddOn>
<AddOn Id="addon1">
<AddonCode>2</AddonCode>
<AddonText>Tremmeseng</AddonText>
<AddonAccNo>200</AddonAccNo>
<AddonPrice>200</AddonPrice>
<AddonPriceOffersText />
<AddonPriceOffers />
</AddOn>
<AddOn Id="addon2">
<AddonCode>9</AddonCode>
<AddonText>Billet</AddonText>
<AddonAccNo>67</AddonAccNo>
<AddonPrice>178</AddonPrice>
<AddonPriceOffersText />
<AddonPriceOffers />
</AddOn>
</AddOns>
Ok - I see:
You are correct. The list nodelist is only iterated once for 1 id. Try wrapping this in afor
loop:
if the id is being iterated
for(int x= 0;x<doc.SelectNodes("TourInfo/AddOns/AddOn").Count;x++) { string addon; XmlNodeList nodeListADDon = doc.SelectNodes("TourInfo/AddOns/AddOn[@Id='addon" + x + "']"); litTotal.Text = ""; litAdd.Text = ""; foreach (XmlNode nodeA in nodeListADDon) { addon = Request.Form["txtAddonAntal" + x]; antal = int.Parse(addon.ToString()); pris = int.Parse(nodeA["AddonPrice"].InnerText); ansver += antal * pris; total += ansver; //Formatted for clarity litTotal.Text += total.ToString() + " kr"; string text = "<table class="table table-condensed"><tr>" text += "<td>" + nodeA["AddonText"].InnerText + "</td>" text += "<td>" + antal.ToString() + "</td>" text += "<td>Pris: " + ansver.ToString() text += " kr</td>" text += "</tr></table><br />"; litAdd.Text = text; } }
UPDATE: I now have access to the OP source and code:
private static void testsss() { XmlDocument doc = new XmlDocument(); doc.Load(@"C:\3rd Party\testsss.xml"); int total = 0; litTotal.Text = ""; litAdd.Text = ""; //Error handling var allAddonNodes = doc.SelectNodes("TourInfo/AddOns/AddOn"); if (allAddonNodes == null) return; for (int x = 0; x < allAddonNodes.Count; x++) { string textBoxname = string.Format("txtAddonAntal{0}", x); XmlNode addonNode = allAddonNodes[x]; //Error handling if (addonNode["AddonPrice"] == null) continue; //Error handling int antal; if (!int.TryParse(Request.Form[textBoxname], out antal)) continue; //Error handling int pris; if (!int.TryParse(addonNode["AddonPrice"].InnerText, out pris)) continue; var ansver = antal * pris; total += ansver; var nodeAAddonText = addonNode["AddonText"]; string nodeAInnerText = nodeAAddonText == null ? "" : nodeAAddonText.InnerText; //This line seems out of place litAdd.Text = string.Format(FormatText, nodeAInnerText, antal, ansver); } litTotal.Text = total + @" kr"; }
FINAL UPDATE: Some ideas about litAdd:
private static void testsss() { //If you have a table included in the markup then run that at server //and use that instead. The method below is a bit of a hack. //We'll use this to write the table markup to a string. This is the hack part StringBuilder sb = new StringBuilder(); HtmlTextWriter writer = new HtmlTextWriter( new StringWriter(sb,CultureInfo.InvariantCulture)); //table to use in the hack HtmlTable table = new HtmlTable(); table.Attributes.Add("class", "table table-condensed"); XmlDocument doc = new XmlDocument(); doc.Load(@"C:\3rd Party\testsss.xml"); int total = 0; litTotal.Text = ""; litAdd.Text = ""; //Error handling var allAddonNodes = doc.SelectNodes("TourInfo/AddOns/AddOn"); if (allAddonNodes == null) return; for (int x = 0; x < allAddonNodes.Count; x++) { //create a new table row HtmlTableRow row = new HtmlTableRow(); // a cell instance to add to the row. HtmlTableCell cell; string textBoxname = string.Format("txtAddonAntal{0}", x); XmlNode addonNode = allAddonNodes[x]; //Error handling if (addonNode["AddonPrice"] == null) continue; //Error handling int antal; if (!int.TryParse(RequestForm[textBoxname], out antal)) continue; //Error handling int pris; if (!int.TryParse(addonNode["AddonPrice"].InnerText, out pris)) continue; var ansver = antal * pris; total += ansver; var nodeAAddonText = addonNode["AddonText"]; string nodeAInnerText = nodeAAddonText == null ? "" : nodeAAddonText.InnerText; cell = new HtmlTableCell(); cell.InnerText = nodeAInnerText; row.Cells.Add(cell); cell = new HtmlTableCell(); cell.InnerText = antal.ToString(); row.Cells.Add(cell); cell = new HtmlTableCell(); cell.InnerText = string.Format("Pris: {0} kr", ansver); row.Cells.Add(cell); //add the row to the table table.Rows.Add(row); } //if the table exists already, then the page render will draw it, the rows and the cells //when the page is rendered. In this hack it is not in the page so we have to render it //ourselves and get the html. This is a hack (but a good one ;) table.RenderControl(writer); litAdd.Text = sb.ToString(); litTotal.Text = total + @" kr"; }
这篇关于嗨伙计们有谁知道我怎么做,让我的Xmlnodelist在Foreach中。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!