i m creating an web application in that i m matching the user answer with my xml answer i have done all code and my code work fine then after i have changed my xml format so now i m unable to read the attribute of my xml file node.
and below is my xml file.
<?xml version="1.0" encoding="utf-8" ?>
<Exam>
<Question number="1" Text="What is IL Code">
<Answer Text="Half compiled, Partially compiled code"> </Answer>
</Question>
<Question number="2" Text="What is JIT">
<Answer Text="IL code to machine language"> </Answer>
</Question>
<Question number="3" Text="What is CLR">
<Answer Text="Heart of the engine , GC , compilation , CAS(Code access security) , CV ( Code verification)"> </Answer>
</Question>
</Exam>
and below is my snipped code.
XmlDocument docQuestionList = new XmlDocument();// Set up the XmlDocument //
docQuestionList.Load(#"E:\ferozProject\WindowsFormsApplication1\WindowsFormsApplication1\QuestionFile.xml"); //Load the data from the file into the XmlDocument //
XmlNodeList QuestionList = docQuestionList.SelectNodes("Exam/Question");
foreach (XmlNode nodexm in QuestionList)
{
if (**nodexm.InnerText.Trim()** == label2.Text)
{
string[] arrUserAnswer = textBox1.Text.Trim().ToLower().Split(' ');
string[] arrXMLAnswer = nodexm.NextSibling.InnerText.Trim().ToLower().Split(' ');
List<string> lststr1 = new List<string>();
foreach (string nextStr in arrXMLAnswer)
{
if (Array.IndexOf(arrUserAnswer, nextStr) != -1)
{
lststr1.Add(nextStr);
}
}
if (lststr1.Count > 0)
{
label4.Visible = true;
label4.Text = "Your Answer is "+ ((100 * lststr1.Count) / arrXMLAnswer.Length).ToString() + "%" + "Correct";
}
else
{
label4.Text = "Your Answer is Wrong";
}
}
}
XmlNodeList QuestionList = docQuestionList.SelectNodes("Exam/Question");
the above line you can see that i have read the question node but inside the question node there is attribute like Text in that my question present you can see in my xml file.
if (nodexm.InnerText.Trim() == label2.Text)
in the above line i m matching the screen display question with my xml file question but i can't do that.label2 is used for displaying the question
help me please.
Try this (using System.Linq;
using System.Xml;
using System.Xml.Linq;)
XDocument map = XDocument.Parse("<Exam> " +
"<Question number= \"1\" Text=\"What is IL Code\">" +
" <Answer Text=\"Half compiled, Partially compiled code\"> </Answer>" +
"</Question>" +
"<Question number=\"2\" Text=\"What is JIT\">" +
" <Answer Text=\"IL code to machine language\"> </Answer>" +
"</Question>" +
"<Question number=\"3\" Text=\"What is CLR\">" +
" <Answer Text=\"Heart of the engine , GC , compilation , CAS(Code access security) , CV ( Code verification)\"> </Answer>" +
"</Question>" +
"</Exam>");
var atts = map.Descendants("Question").Attributes().Where(a => a.Name == "Text").ToList();
var QuestionList = map.Descendants("Question").ToList();
foreach (XElement nodexm in QuestionList)
{
if((string)nodexm.Attributes("Text").FirstOrDefault()== label2.Text)
{
string[] arrUserAnswer = textBox1.Text.Trim().ToLower().Split(' ');
string[] arrXMLAnswer = nodexm.Elements("Answer").SelectMany(o => ((string)o.Attribute("Text")).Split(',')).ToArray();
List<string> lststr1 = new List<string>();
foreach (string nextStr in arrXMLAnswer)
{
if (Array.IndexOf(arrUserAnswer, nextStr) != -1)
{
lststr1.Add(nextStr);
}
}
if (lststr1.Count > 0)
{
label4.Visible = true;
label4.Text = "Your Answer is "+ ((100 * lststr1.Count) / arrXMLAnswer.Length).ToString() + "%" + "Correct";
}
else
{
label4.Text = "Your Answer is Wrong";
}
}
}
Related
I'm working on a C# Desktop App and in a module I display the info of a xml file into a listview, I coded my solution with Linq to XML like this
string path = "prueba.xml";
listView1.Items.Clear();
listView1.Columns.Add(path, 400);
XElement doc = XElement.Load(path);
var result = from persona in doc.Elements("persona")
select new{
nombre = Convert.ToString(persona.Element("nombre").Value).Trim(),
ocupacion = Convert.ToString(persona.Element("ocupacion").Value).Trim()
};
foreach (var persona in result)
{
/*ListViewItem item = new ListViewItem(persona.nombre);
item.SubItems.Add(persona.ocupacion);*/
ListViewItem nombre = new ListViewItem("<nombre> " + persona.nombre + " </nombre>");
ListViewItem ocupacion = new ListViewItem("<ocupacion> " + persona.ocupacion + " </ocupacion>");
listView1.Items.Add("<persona>");
listView1.Items.Add(nombre);
listView1.Items.Add(ocupacion);
listView1.Items.Add("</persona>");
listView1.Items.Add("");
}
}
}
and it works very fine, as you can see there are items in the listview that represents the nodes of the xml file, but those items are specific for this xml file
<?xml version="1.0" encoding="utf-8" ?>
<personas>
<persona>
<nombre>Pablo el primero</nombre>
<ocupacion>Programador Cliente-Servidor</ocupacion>
</persona>
<persona>
<nombre>Pablo el segundo</nombre>
<ocupacion>Programador Web</ocupacion>
</persona>
</personas>
as you can see in the C# code, it fits for the xml file above but if I retrieve another xml file with different nodes name like for example
<juego>
<juegos>
<name id="ac"> God of War III </name>
...
</juegos>
</juego>
my code wont show me the nodes <juegos>...</juegos> because it will still display the <person>...</person> nodes because it was created to display only the node <person>...</person> so my question: is there a way to show the information of a xml file into a listview using Linq to XML and at the same time display the info as it is coded in the xml file?
I want to know if I can display this format in teh listview:
<?xml version="1.0" encoding="utf-8" ?>
<personas>
<persona>
<nombre>Pablo el primero</nombre>
<ocupacion>Programador Cliente-Servidor</ocupacion>
</persona>
<persona>
<nombre>Pablo el segundo</nombre>
<ocupacion>Programador Web</ocupacion>
</persona>
</personas>
You don't have to use linq to do it, you can simply query the elements of your document.
listView1.View = System.Windows.Forms.View.Details;
listView1.AutoArrange = false;
listView1.Alignment = ListViewAlignment.Left;
listView1.Items.Clear();
listView1.Columns.Add("XML",400);
string xml =
#"<?xml version=""1.0"" encoding=""utf-8"" ?>
<personas>
<persona>
<nombre>Pablo el primero</nombre>
<ocupacion>Programador Cliente-Servidor</ocupacion>
</persona>
<persona>
<nombre>Pablo el segundo</nombre>
<ocupacion>Programador Web</ocupacion>
</persona>
</personas>";
XDocument doc = XDocument.Parse(xml);
List<ListViewItem> ItemList = new List<ListViewItem>();
foreach (XElement elem in doc.Elements())
{
AddNewItemToList(ItemList, elem);
}
listView1.Items.AddRange(ItemList.ToArray());
...
private void AddNewItemToList(List<ListViewItem> ItemList, XElement elem)
{
string attributes = "";
if (elem.HasAttributes)
{
foreach (XAttribute attr in elem.Attributes())
{
attributes += " " + attr.Name + "=\"" + attr.Value + "\"";
}
}
if (elem.HasElements)
{
ItemList.Add(new ListViewItem("<" + elem.Name + attributes + ">"));
foreach (XElement childElem in elem.Elements())
{
AddNewItemToList(ItemList, childElem);
}
ItemList.Add(new ListViewItem("</" + elem.Name + ">"));
}
else
{
ItemList.Add(new ListViewItem("<" + elem.Name + attributes + ">" + elem.Value + "</" + elem.Name + ">"));
}
}
Hi you is it possible to create a dynamica xml with xdocument I've been trying but it appears that it returns an exception about having a wrong structure
My code is the following
public string ReadTest(Stream csvFile)
{
XDocument responseXml = new XDocument( new XDeclaration("1.0", "utf-8", "yes"));
try
{
if ( csvFile != null || csvFile.Length!=0)
{
responseXml.Add(new XElement("root"));
//using(CsvFileReader reader=new CsvFileReader(File.OpenRead(#"C:\Users\toshibapc\Documents\Visual Studio 2013\Projects\WCFLecturaCSV\WCFLecturaCSV\App_Data\archivo.csv"))){
using (CsvFileReader reader = new CsvFileReader(csvFile))
{
CsvRow row = new CsvRow();
List<String> headers = new List<string>();
while (reader.ReadRow(row))
{
int cont = 0;
XElement dato = new XElement("AccountInfos", new XElement("Info"));
XElement datos=null;
foreach (String s in row)
{
if(s.Equals("AccountIDToMove", StringComparison.OrdinalIgnoreCase)|| s.Contains("AccountNameToMove") || s.Contains("NewParentAccountID") || s.Contains("NewParentAccountName")){
headers.Add(s);
}
else{
if (s != String.Empty)
{
datos = new XElement(headers[cont], s); //.Append("<" + headers[cont] + ">" + s + "<" + headers[cont] + "/>");
dato.Add(datos);
}
}
cont++;
}
if (headers.Count == 4 && datos != null)
responseXml.Add(dato);
} // fin de while
}
} // Check if no file i sent or not info on file
}
catch (Exception ex) {
//oError = ex.Message;
}
return responseXml.ToString();
}
What i would like to acomplish by using this code is to get an xml like this
<xml version="1.0">
<root>
<AccountInfos>
<Info>
<AccountIDToMove>312456</AccountIDToMove>
<AccountNameToMove>Burger Count</AccountNameToMove>
<NewParentAccountID>453124</NewParentAccountID>
<NewParentAccountName> Testcom sales 1</NewParentAccountName>
</Info>
<Info>
<AccountIDToMove>874145</AccountIDToMove>
<AccountNameToMove>Mac Count</AccountNameToMove>
<NewParentAccountID>984145</NewParentAccountID>
<NewParentAccountName> Testcom sales 1</NewParentAccountName>
</Info>
</AccountInfos>
</root>
For any answer or help thank you so much
You are adding multiple roots to your document. You initially add one here:
responseXml.Add(new XElement("root"));
And later add more root elements in a loop here:
responseXml.Add(dato);
However, each XML document must have exactly one single root element. Thus you probably want to do:
responseXml.Root.Add(dato);
I am trying to remove the last node from an XML file, but cannot find any good answers for doing this. Here is my code:
XmlReader x = XmlReader.Create(this.PathToSpecialFolder + #"\" + Application.CompanyName + #"\" + Application.ProductName + #"\Recent.xml");
int c = 0;
while (x.Read())
{
if (x.NodeType == XmlNodeType.Element && x.Name == "Path")
{
c++;
if (c <= 10)
{
MenuItem m = new MenuItem() { Header = x.ReadInnerXml() };
m.Click += delegate
{
};
openRecentMenuItem.Items.Add(m);
}
}
}
x.Close();
My XML node structure is as follows...
<RecentFiles>
<File>
<Path>Text Path</Path>
</File>
</RecentFiles>
In my situation, there will be ten nodes maximum, and each time a new one is added, the last must be removed.
You can try this
XmlDocument doc = new XmlDocument();
doc.Load(fileName);
XmlNodeList nodes = doc.SelectNodes("/RecentFiles/File");
nodes[nodes.Count].ParentNode.RemoveChild(nodes[nodes.Count]);
doc.Save(fileName);
It sounds like you want something like:
var doc = XDocument.Load(path);
var lastFile = doc.Descendants("File").LastOrDefault();
if (lastFile != null)
{
lastFile.Remove();
}
// Now save doc or whatever you want to do with it...
So I'm trying to deserialize the following XML Document into multiple objects of my custom type (ItemModel). Since I'm developing for the Windows 8 platform, I've been hitting a lot of blocks due to library incompatibilities. What I'm trying to do is deserialize each ItemModel into an object than add them to a List of some sort. From what I have, the code runs but the list is not populating.
<?xml version="1.0" encoding="utf-8" ?>
<Items>
<ItemModel>
<ID>0</ID>
<Name>Apple</Name>
<Category>Compost</Category>
<ImageWidth>67</ImageWidth>
<ImageHeight>100</ImageHeight>
<Description>An Apple is a compost item that....</Description>
<ImagePath>Graphics\\apple.png</ImagePath>
</ItemModel>
<ItemModel>
<ID>0</ID>
<Name>Water Bottle</Name>
<Category>Mixed Containers</Category>
<ImageWidth>67</ImageWidth>
<ImageHeight>100</ImageHeight>
<Description>A Water bottle is a mixed container item that...</Description>
<ImagePath>Graphics\\Bottle.png</ImagePath>
</ItemModel>
</Items>
Note: I'm also experiencing some trouble using the XmlReader. It's the reader us equal to null even after I use XmlReader.Create().
Thank you.
if you are reading from a .xml file and displaying in Web browser and your code behind is C#, you can do something like this :
protected void Page_Load(object sender, EventArgs e)
{
ReadXmlFile(Server.MapPath("~/XMLFiles/Items.xml"));
}
private void ReadXmlFile(string fileName)
{
string parentElementName = "";
string childElementName = "";
string childElementValue = "";
bool element = false;
lblMsg.Text = "";
XmlTextReader xReader = new XmlTextReader(fileName);
while (xReader.Read())
{
if (xReader.NodeType == XmlNodeType.Element)
{
if (element)
{
parentElementName = parentElementName + childElementName + "<br>";
}
element = true;
childElementName = xReader.Name;
}
else if (xReader.NodeType == XmlNodeType.Text | xReader.NodeType == XmlNodeType.CDATA)
{
element = false;
childElementValue = xReader.Value;
lblMsg.Text = lblMsg.Text + "<b>" + parentElementName + "<br>" + childElementName + "</b><br>" + childElementValue;
parentElementName = "";
childElementName = "";
}
}
xReader.Close();
}
}
i m creating an window application in that i have one label for display the question and one textbox for answering the respective question.then 3 button for check the answer with my xml file and one button for next question and one button for previous question.
this is my xml file
<?xml version="1.0" encoding="utf-8" ?>
<Exam>
<Question number="1" Text="What is IL Code">
<Answer Text="Half compiled, Partially compiled code"> </Answer>
</Question>
<Question number="2" Text="What is JIT">
<Answer Text="IL code to machine language"> </Answer>
</Question>
<Question number="3" Text="What is CLR">
<Answer Text="Heart of the engine , GC , compilation , CAS(Code access security) , CV ( Code verification)"> </Answer>
</Question>
</Exam>
now on button click i want to check the user answer with my xml answer and this part i have done but little problem whenever for the first question that is what is il code for this my code work properly and when the question is changed then my code unable to take the second question every time it take first question and compare with this so how can i achieve this?
below is my snipped code
string[] arrUserAnswer = textBox1.Text.Trim().ToLower().Split(' ');
do
{
XmlReader reader = XmlReader.Create(#"E:\ferozProject\WindowsFormsApplication1\WindowsFormsApplication1\QuestionFile.xml");
reader.Read();
reader.ReadToFollowing("Question");
reader.MoveToContent();
que = reader.GetAttribute("Text");
reader.ReadToFollowing("Answer");
reader.MoveToContent();
string[] arrXMLAnswer = reader.GetAttribute("Text").ToString().Trim().ToLower().Split(' ');
List<string> lststr1 = new List<string>();
if (label2.Text == que)
{
abc = 1;
foreach (string nextStr in arrXMLAnswer)
{
if (Array.IndexOf(arrUserAnswer, nextStr) != -1)
{
lststr1.Add(nextStr);
}
}
if (lststr1.Count > 0)
{
label4.Visible = true;
label4.Text = "Your Answer is " + ((100 * lststr1.Count) / arrXMLAnswer.Length).ToString() + "%" + "Correct";
}
else
{
textBox1.Text = "0 %";
}
}
else
{
reader.ReadToNextSibling("Question");
}
} while (abc <= 0);
abc = 0;
i have also used the second another method for that but in which the code unable to find out my question as because of i have written the question inside the question node, below is my another code.
XmlDocument docQuestionList = new XmlDocument();// Set up the XmlDocument //
docQuestionList.Load(#"E:\ferozProject\WindowsFormsApplication1\WindowsFormsApplication1\QuestionFile.xml"); //Load the data from the file into the XmlDocument //
XmlNodeList QuestionList = docQuestionList.SelectNodes("Exam/Question");
foreach (XmlNode nodexm in QuestionList)
{
string obj = nodexm.SelectNodes("Text").ToString();
if (obj == label2.Text)
{
string[] arrUserAnswer = textBox1.Text.Trim().ToLower().Split(' ');
string[] arrXMLAnswer = nodexm.NextSibling.InnerText.Trim().ToLower().Split(' ');
List<string> lststr1 = new List<string>();
foreach (string nextStr in arrXMLAnswer)
{
if (Array.IndexOf(arrUserAnswer, nextStr) != -1)
{
lststr1.Add(nextStr);
}
}
if (lststr1.Count > 0)
{
label4.Text = "Your Answer is " + ((100 * lststr1.Count) / arrXMLAnswer.Length).ToString() + "%" + "Correct";
}
else
{
textBox1.Text = "0 %";
}
}
}
please help me in any one method
Something like:
//setup the doc
string fn="C:\\bla.xml";
XmlDocument xmlDocument=new XmlDocument();
xmlDocument.Load(fn);
XmlNode root=xmlDocument.DocumentElement;
//get the node
XmlNode answerNode=root.SelectSingleNode("//Question[#number="+num+"]/Answer");
//get the value
string attrName="Text";
XmlAttribute atr=answerNode.Attributes.GetNamedItem(attrName) as XmlAttribute;
if (atr!=null){
string answer=atr.value;
}
will make your life a lot easier. Notice and provide the num variable, and do some null checks.