randomly Select XML nodes with same names - c#

I have this XML file:
<Main>
<QA>
<question>What is your favorite color?</question>
<!--pick random!-->
<answer>Blue</answer>
<answer>Red</answer>
<answer>Green</answer>
<answer>Yellow</answer>
</QA>
<QA>
<question>What is your favorite programming language?</question>
<!--pick random!-->
<answer>PHP</answer>
<answer>C#</answer>
<answer>Java</answer>
<answer>VB.Net</answer>
</QA>
I want when the user enter one of the question that specified in above xml file, program pick a random answer from nodes.
For example when user enters "What is your favorite programming language?" in TextBox,
program must generate PHP,C#,Java or VB.Net randomly.
this is my code but not correct:
XmlDocument xml = new XmlDocument();
xml.Load("QA.xml");
XmlNodeList xList = xml.SelectNodes("Main/QA");
foreach (XmlNode xn in xList)
{
string Question = xn["question"].InnerText;
if (Question == txtQuestion.Text)
{
XmlNodeList answerlist = xml.SelectNodes("Main/QA/answer");
foreach (XmlNode ans in answerlist)
{
Console.WriteLine(ans.InnerText);
}
}
}
Output:
Blue
Red
Green
Yellow
PHP
C#
Java
VB.Net
Please help.

Try this:
XmlDocument xml = new XmlDocument();
xml.Load("QA.xml");
XmlNodeList xList = xml.SelectNodes("Main/QA");
foreach (XmlNode xn in xList)
{
string Question = xn["question"].InnerText;
if (Question == txtQuestion.Text)
{
XmlNodeList answerlist = xn.SelectNodes("./answer");
foreach (XmlNode ans in answerlist
.Cast<XmlNode>()
.OrderBy(elem => Guid.NewGuid()))
{
Console.WriteLine(ans.InnerText);
}
}
}

using Linq To Xml,
Random rnd = new Random();
var xDoc = XDocument.Load("QA.xml");
var question = xDoc.Descendants("QA")
.First(d => d.Element("question").Value == txtQ.Text);
var answer = question.Elements()
.Skip(rnd.Next(0, question.Elements().Count()))
.First().Value;

Related

HasChildNodes (.Net, XmlDocument ) always reported "true"

I would like to distinguish these two cases - but HasChildNode returns in both cases "true" and the number of childNodes is 1 in both cases.
<eventid>45072</eventid>
<titles>
<title>kabel eins late news</title>
</titles>
Here ist my sample code and output.
XmlDocument doc = new XmlDocument();
doc.LoadXml(xmlString);
XmlNodeList xnList = doc.SelectNodes("/epg/programme");
foreach (XmlNode n in xnList) {
foreach (XmlNode n1 in n.ChildNodes) {
if (n1.HasChildNodes) {
AppendText($"n1 has {n1.ChildNodes.Count} ChildNodes");
foreach (XmlNode n2 in n1.ChildNodes) {
Append(41,n2.Name, n2.InnerText);
}
}
else {
// this will never called
Append(46,n1.Name, n1.InnerText);
}
}
}
<?xml version="1.0" encoding="ISO-8859-1"?>
<epg Ver="1">
<programme>
<eventid>45072</eventid>
<titles>
<title>kabel eins late news</title>
</titles>
</programme>
</epg>
Output:
n1 has 1 ChildNodes
41:#text:45072
n1 has 1 ChildNodes
41:title:kabel eins late news
Thanks to #DavidBrowne-Microsoft I have changed my code to XDocument and a little bit Linq to XML
XDocument doc = XDocument.Parse(xmlString);
var programme = doc.Descendants("programme").Select(p => p);
foreach (XElement p in programme) {
foreach (var attr in p.Attributes()){
AppendText($"{attr.Name} {attr.Value}");
}
foreach (XElement ele in p.Elements()) {
if (ele.HasElements) {
foreach (XElement child in ele.Elements()) {
AppendText($"{child.Name} {child.Value}");
}
}
else {
AppendText($"{ele.Name} {ele.Value}");
}
}
}
I found only a few beginner examples so here is this snippet for the start.

Xpath expression to get paragraphs from rssfeed

http://lankacnews.com/sinhala/feed/
this is a feed address of a news web site. It contains 11 latest news.
I want to get those news from that rss feed and show them on my program. It contains news like this.
http://i44.tinypic.com/a3cxsw.png
between content : encoded tag.
I want to get it and show it in my application.
This is the code I have used..
XmlDocument lkcnws = new XmlDocument();
lkcnws.Load(#"http://lankacnews.com/sinhala/feed/");
textBox1.Text = lkcnws.OuterXml;
XmlNodeList ndlst;
XmlNode root = lkcnws.DocumentElement;
ndlst = root.SelectNodes("//p");
foreach (XmlNode nd in ndlst)
{
textBox2.Text += nd.OuterXml;
}
but it does not work. Whats the wrong with this code and how can I solve this ?
Try this:
void test() {
XmlDocument lkcnws = new XmlDocument();
lkcnws.Load("http://lankacnews.com/sinhala/feed/");
textBox1.Text = lkcnws.OuterXml;
XmlNodeList ndlst = lkcnws.SelectNodes("//category['#*']"); // it's null with p
foreach (XmlNode nd in ndlst)
{
textBox2.Text += nd.InnerXml;
}
}

How to get xml element values using XmlNodeList in c#

I need to store the element values which are inside the nodes "member" . I have tried the following code but I can't achieve it. How to get the values. Any help would be appreciated
XML:
<ListInventorySupplyResponse xmlns="http://mws.amazonaws.com/FulfillmentInventory/2010-10-01/">
<ListInventorySupplyResult>
<InventorySupplyList>
<member>
<SellerSKU>043859634910</SellerSKU>
<FNSKU>X000IA4045</FNSKU>
<ASIN>B005YV4DJO</ASIN>
<Condition>NewItem</Condition>
<TotalSupplyQuantity>7</TotalSupplyQuantity>
<InStockSupplyQuantity>7</InStockSupplyQuantity>
<EarliestAvailability>
<TimepointType>Immediately</TimepointType>
</EarliestAvailability>
<SupplyDetail>
</SupplyDetail>
</member>
</InventorySupplyList>
</ListInventorySupplyResult>
<ResponseMetadata>
<RequestId>58c9f4f4-6f60-496a-8d71-8fe99ce301c9</RequestId>
</ResponseMetadata>
</ListInventorySupplyResponse>
C# Code:
string a = Convert.ToString(oInventorySupplyRes.ToXML());
XmlDocument oXdoc = new XmlDocument();
oXdoc.LoadXml(a);
XmlNodeList oInventorySupplyListxml = oXdoc.SelectNodes("//member");
foreach (XmlNode itmXml in oInventorySupplyListxml)
{
// var cond = itmXml.InnerXml.ToString();
var asinVal = itmXml.SelectSingleNode("ASIN").Value;
var TotalSupplyQuantityVal = itmXml.SelectSingleNode("TotalSupplyQuantity").Value;
}
ResultView : "Enumeration yielded no results" and count = 0;
Edit 1:
string a = Convert.ToString(oInventorySupplyRes.ToXML());
var status = oInventorySupplyResult.InventorySupplyList;
XmlDocument oXdoc = new XmlDocument();
var doc = XDocument.Parse(a);
var r = doc.Descendants("member")
.Select(member => new
{
ASIN = member.Element("ASIN").Value,
TotalSupplyQuantity = member.Element("TotalSupplyQuantity").Value
});
private string mStrXMLStk = Application.StartupPath + "\\Path.xml";
private System.Xml.XmlDocument mXDoc = new XmlDocument();
mXDoc.Load(mStrXMLStk);
XmlNode XNode = mXDoc.SelectSingleNode("/ListInventorySupplyResult/InventorySupplyList/member");
if (XNode != null)
{
int IntChildCount = XNode.ChildNodes.Count;
for (int IntI = 1; IntI <= IntChildCount ; IntI++)
{
string LocalName = XNode.ChildNodes[IntI].LocalName;
XmlNode Node = mXDoc.SelectSingleNode("/Response/" + LocalName);
// Store Value in Array assign value by "Node.InnerText"
}
}
Try This Code. Its Worked
try using this xpath
string xPath ="ListInventorySupplyResponse/ListInventorySupplyResult
/InventorySupplyList/member"
XmlNodeList oInventorySupplyListxml = oXdoc.SelectNodes(xpath);
when you do "//member", then, the code is trying to look for element named member from the root level, which is not present at the root level, rather it is nested beneath few layers.
I think this will help you..
string a = Convert.ToString(oInventorySupplyRes.ToXML());
XmlDocument oXdoc = new XmlDocument();
oXdoc.LoadXml(a);
XmlNodeList fromselectors;
XmlNodeList toselectors;
XmlElement root = oXdoc.DocumentElement;
fromselectors = root.SelectNodes("ListInventorySupplyResult/InventorySupplyList/member/ASIN");
toselectors = root.SelectNodes("ListInventorySupplyResult/InventorySupplyList/member/TotalSupplyQuantity");
foreach (XmlNode m in fromselectors)
{
you will have value in `m.InnerXml` use it whereever you want..
}
foreach (XmlNode n in toselectors)
{
you will have value in `n.InnerXml` use it whereever you want..
}

Loop through XML with XmlDocument

I'm new to using Linq and XMLDocument.
I have a simple XML file and I want to loop through all of the elements and print the tag and value. I don't want to use the XML Tags when looping through. this is what I have so far.
XML file:
<?xml version="1.0" encoding="UTF-8"?>
<Step1>One
<Step2>Two
<Step3>Three
<Step4>Four
</Step4>
</Step3>
</Step2>
</Step1>
C# Code
private void StartIt()
{
System.Xml.XmlDocument xd = new System.Xml.XmlDocument();
xd.Load(#"C:\Projects\GetXML\testLayers.xml");
XmlNodeList nl = xd.SelectNodes("Layer1");
foreach (XmlNode xnode in nl)
{
Console.WriteLine(xnode.Name + " = " + xnode.InnerText); // + " " + xnode.InnerXml);
}
}
Results:
Step1 = One
Two
Three
Four
What I want:
Step1 = One
Step2 = Two
Step3 = Three
Step4 = Four
Any suggestions?
With a little help of Linq,
XmlDocument doc = new XmlDocument();
doc.Load(fname);
var nodes = doc.SelectNodes("//*[text()]")
.Cast<XmlNode>()
.Select(n => new {
Name= n.Name,
Value = n.SelectSingleNode("text()").Value
})
.ToList();
// System.Xml.XmlDocument version
XmlDocument xd = new XmlDocument();
xd.Load(#"C:\Projects\GetXML\testLayers.xml");
foreach (XmlElement step in xd.SelectNodes("//*"))
{
Console.WriteLine("{0} = {1}", step.Name,
step.SelectSingleNode("text()").Value);
}
// System.Xml.Linq.XDocument version
XDocument xdLinq = XDocument.Load(#"C:\Projects\GetXML\testLayers.xml");
foreach (XElement step in xdLinq.XPathSelectElements("//*"))
{
Console.WriteLine("{0} = {1}", step.Name,
step.Nodes().Where(n => n.NodeType == XmlNodeType.Text).FirstOrDefault());
}
You can do the same using LINQ to XML and XDocument class:
var xDoc = XDocument.Load("Input.txt");
foreach (var e in xDoc.Descendants())
{
Console.WriteLine("{0} = {1}", e.Name, e.Nodes().OfType<XText>().First().Value.Trim());
}

How can I get node I want from XML

<?xml version="1.0" encoding="utf-8" ?>
<Root>
<Fruits>
<Fruit>hahahaha</Fruit>
</Fruits>
</Root>
If I try this code,
string[] cFruitName; int i=0;
XmlDocument a= new XmlDocument();
a.LoadXml(getXML());
foreach (XmlNode xn in a)
{
cFruitName[i] = xn.Text;
i++;
}
but I am getting null for xn.Text.
All I want is to get values of fruit, like "hahahah" in this example.
Edit
I changed my XML now.
var xml = XDocument.Parse(getXML());
var fruits = xml.Descendants("fruit").Select(n => n.Value);
Console.WriteLine(string.Join(Environment.NewLine, fruits));
prints
hahahaha
use XPath for .net 2.0 version
XmlDocument xml = new XmlDocument();
xml.LoadXml(getXML());
XmlNodeList fruits = xml.SelectNodes("//fruit");
foreach (XmlNode fruit in fruits)
{
Console.WriteLine (fruit.InnerText);
}
prints the same
Try this:
string[] cFruitName; int i=0;
XmlDocument a= new XmlDocument();
a.LoadXml(getXML());
foreach (XmlNode xn in a.Where(x=>x.Text != null))
{
cFruitName[i] = xn.Text;
i++;
}
or
string[] cFruitName; int i=0;
XmlDocument a= new XmlDocument();
a.LoadXml(getXML());
foreach (XmlNode xn in a.Where(x=>x.Name == "fruit"))
{
cFruitName[i] = xn.Text;
i++;
}

Categories

Resources