XML values wont read to string - c#

I am having difficulties with my XML code it doesn't seem to saving and when I print it out nothing happens. I am not sure what is wrong because before it would load into my listbox but it would load incorrectly. The code is below and the purpose of my XML reading is the store the values in a list and then get a selected tag and add it to a listbox.
String workingDir = Directory.GetCurrentDirectory();
XmlTextReader textReader = new XmlTextReader(workingDir + #"\XML.xml");
textReader.Read();
XmlNodeType type;
while (textReader.Read())
{
textReader.MoveToElement();
type = textReader.NodeType;
if (type == XmlNodeType.Text)
{
if (textReader.Name == "Code")
{
textReader.Read();
code = textReader.Value;
Console.WriteLine(code);
}
if (textReader.Name == "Name")
{
textReader.Read();
name = textReader.Value;
Console.WriteLine(name);
}
if (textReader.Name == "Semester")
{
textReader.Read();
semester = textReader.Value;
Console.WriteLine(semester);
}
if (textReader.Name == "Prerequisite")
{
textReader.Read();
preReq = textReader.Value;
Console.WriteLine(code);
}
if (textReader.Name == "LectureSlot")
{
textReader.Read();
lSlot = textReader.Value;
Console.WriteLine(lSlot);
}
if (textReader.Name == "TutorialSlot")
{
textReader.Read();
tSlot = textReader.Value;
Console.WriteLine(tSlot);
}
if (textReader.Name == "Info")
{
textReader.Read();
info = textReader.Value;
module.Add(new modules(name, code, semester, tSlot, lSlot, info, preReq));
}
}
foreach (object o in module)
{
modules m = (modules)o;
String hold = m.mName;
selectionBox.Items.Add(hold);
}
}

The thing is that you look for type == XmlNodeType.Text, but text nodes does not have any name, no text nodes will match textReader.Name == "Code".
You need to store textReader.Name from the last node with type == XmlNodeType.Element in a variable and use the stored name when you find the XmlNodeType.Text node.

I think the most likely reason is that in each of your if statements, you are using textReader.Read(). For most Readers this will read the next item, not the current.
As the other answer has said, you need to look at the element for the Name and then read for the value.
Consider something like this instead:
while (textReader.Read())
{
textReader.MoveToElement();
type = textReader.NodeType;
if (type == XmlNodeType.Element)
{
textReader.Read();
switch( textReader.Name )
{
case "Code":
code = textReader.Value;
break;
case "Name":
name = textReader.Value;
break;
//SNIP
case "Info":
info = textReader.Value;
module.Add(new modules(name, code, semester, tSlot, lSlot, info, preReq));
break;
default:
//Whatever you do here
break;
}
Console.WriteLine(textReader.Value);
}
foreach (object o in module)
{
modules m = (modules)o;
String hold = m.mName;
selectionBox.Items.Add(hold);
}
}
This way your XMLTextReader is only reading one node per iteration, and you have a lot fewer if checks - this is the situation a switch case was designed for.

Related

Get string data Xpath

I need help to get data from the site. I use geckofx in my application. I want it to retrieve text data from the xpath location after loading the page
XPathResult xpathResult = geckoWebBrowser1.Document.EvaluateXPath("/html/body/table[3]/tbody/tr[1]/td[2]/a[1]");
IEnumerable<GeckoNode> foundNodes = xpathResult.GetNodes();
How to download data as text?
It looks like you are struggling to retrieve the text from the GeckoFX objects.
Here are a few calls and operations that should get you started:
//get by XPath
XPathResult xpathResult = _browser.Document.EvaluateXPath("//*[#id]/div/p[2]");
var foundNodes = xpathResult.GetNodes();
foreach (var node in foundNodes)
{
var x = node.TextContent; // get text text contained by this node (including children)
GeckoHtmlElement element = node as GeckoHtmlElement; //cast to access.. inner/outerHtml
string inner = element.InnerHtml;
string outer = element.OuterHtml;
//iterate child nodes
foreach (var child in node.ChildNodes)
{
}
}
//get by id
GeckoHtmlElement htmlElementById = _browser.Document.GetHtmlElementById("mw-content-text");
//get by tag
GeckoElementCollection byTag = _browser.Document.GetElementsByTagName("input");
foreach (var ele in byTag)
{
var y = ele.GetAttribute("value");
}
//get by class
var byClass = _browser.Document.GetElementsByClassName("input");
foreach (var node in byClass)
{
//...
}
//cast to a different object
var username = ((GeckoInputElement)_browser.Document.GetHtmlElementById("yourUsername")).Value;
//create new object from DomObject
var button = new GeckoButtonElement(_browser.Document.GetElementById("myBtn").DomObject);
public string extract(string xpath, string type)
{
string result = string.Empty;
GeckoHtmlElement elm = null;
GeckoWebBrowser wb = geckoWebBrowser1;//(GeckoWebBrowser)GetCurrentWB();
if (wb != null)
{
elm = GetElement(wb, xpath);
if (elm != null)
//UpdateUrlAbsolute(wb.Document, elm);
if (elm != null)
{
switch (type)
{
case "html":
result = elm.OuterHtml;
break;
case "text":
if (elm.GetType().Name == "GeckoTextAreaElement")
{
result = ((GeckoTextAreaElement)elm).Value;
}
else
{
result = elm.TextContent.Trim();
}
break;
case "value":
result = ((GeckoInputElement)elm).Value;
break;
default:
result = extractData(elm, type);
break;
}
}
}
return result;
}
private string extractData(GeckoHtmlElement ele, string attribute)
{
var result = string.Empty;
if (ele != null)
{
var tmp = ele.GetAttribute(attribute);
/*if (tmp == null)
{
tmp = extractData(ele.Parent, attribute);
}*/
if (tmp != null)
result = tmp.Trim();
}
return result;
}
private object GetCurrentWB()
{
if (tabControl1.SelectedTab != null)
{
if(tabControl1.SelectedTab.Controls.Count > 0)
//if (tabControl1.SelectedTab.Controls.Count > 0)
{
Control ctr = tabControl1.SelectedTab.Controls[0];
if (ctr != null)
{
return ctr as object;
}
}
}
return null;
}
private GeckoHtmlElement GetElement(GeckoWebBrowser wb, string xpath)
{
GeckoHtmlElement elm = null;
if (xpath.StartsWith("/"))
{
if (xpath.Contains("#class") || xpath.Contains("#data-type"))
{
var html = GetHtmlFromGeckoDocument(wb.Document);
HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument();
doc.LoadHtml(html);
var node = doc.DocumentNode.SelectSingleNode(xpath);
if (node != null)
{
var currentXpath = "/" + node.XPath;
elm = (GeckoHtmlElement)wb.Document.EvaluateXPath(currentXpath).GetNodes().FirstOrDefault();
}
}
else
{
elm = (GeckoHtmlElement)wb.Document.EvaluateXPath(xpath).GetNodes().FirstOrDefault();
}
}
else
{
elm = (GeckoHtmlElement)wb.Document.GetElementById(xpath);
}
return elm;
}
private string GetHtmlFromGeckoDocument(GeckoDocument doc)
{
var result = string.Empty;
GeckoHtmlElement element = null;
var geckoDomElement = doc.DocumentElement;
if (geckoDomElement is GeckoHtmlElement)
{
element = (GeckoHtmlElement)geckoDomElement;
result = element.InnerHtml;
}
return result;
}
private void button5_Click(object sender, EventArgs e)
{
var text = extract("/html/body/table[3]/tbody/tr[1]/td[2]/a[2]", "text");
MessageBox.Show(text);
}
I also insert the code from which I used a little longer code but it also works. maybe someone will need it. The creator of the code is Đinh Công Thắng, Web Automation App,
Regards

How to get value from a specific child element in XML using XmlReader?

Here's the XML string.
<?xml version="1.0" encoding="utf-16"?>
<questionresponses>
<question id="dd7e3bce-57ee-497a-afe8-e3d8d25e2671">
<text>Question 1?</text>
<response>abcdefg</response>
<correctresponse>123</correctresponse>
</question>
<question id="efc43b1d-048f-4ba9-9cc0-1cc09a7eeaf2">
<text>Question 2?</text>
<response>12345678</response>
<correctresponse>123</correctresponse>
</question>
</questionresponses>
So how could I get value of <response> element by given question Id? Say, if I give id value = "dd7e3bce-57ee-497a-afe8-e3d8d25e2671", I'd like to have string value abcdefg returned as result.
var xmlstr = "content from above xml example";
using (var reader = XmlReader.Create(new StringReader(xmlstr)))
{
while(reader.Read())
{
if(reader.IsStartElement())
{
var attr = reader["id"];
if(attr != null && attr == "dd7e3bce-57ee-497a-afe8-e3d8d25e2671")
{
if(reader.ReadToDescendant("response"))
{
result = reader.Value; // <= getting empty string? So what's wrong?
break;
}
}
}
}
}
you might need to do like this , problem i think is reader is not moving to text and because of that you are getting empty
if(reader.ReadToDescendant("response"))
{
reader.Read();//this moves reader to next node which is text
result = reader.Value; //this might give value than
break;
}
Above one is working for me you can try out at your end
I would use LINQ2XML..
XDocument doc=XDocument.Parse(xmlstr);
String response=doc.Elements("question")
.Where(x=>x.Attribute("id")==id)
.Single()
.Element("response")
.Value;
if (reader.NodeType == XmlNodeType.Element)
{
if(reader.Name == "response")
{
reader.read();
var res = reader.Value;
}
}
//it works for me !!!!
You can use this function to get a response for specific questions from XML stored in QuestionXML.xml.
private string getResponse(string questionID)
{
string response = string.Empty;
using (StreamReader sr = new StreamReader("QuestionXML.xml", true))
{
XmlDocument xmlDoc1 = new XmlDocument();
xmlDoc1.Load(sr);
XmlNodeList itemNodes = xmlDoc1.GetElementsByTagName("question");
if (itemNodes.Count > 0)
{
foreach (XmlElement node in itemNodes)
{
if (node.Attributes["id"].Value.ToString() == questionID.Trim())
{
response = node.SelectSingleNode("response").InnerText;
break;
}
}
}
}
return response;
}

Specific XML Attribute values into Class List

Its been a while since I last tried to program and has never worked with XML before. I have a internal website that display XML
<Source>
<AllowsDuplicateFileNames>YES</AllowsDuplicateFileNames>
<Description>The main users ....</Description>
<ExportSWF>FALSE</ExportSWF>
<HasDefaultPublishDir>NO</HasDefaultPublishDir>
<Id>28577db1-956c-41f6-b775-a278c39e20a1</Id>
<IsAssociated>YES</IsAssociated>
<LogoURL>http://servername:8080/logos/9V0.png</LogoURL>
<Name>Portal1</Name>
<RequiredParameters>
<RequiredParameter>
<Id>user_name</Id>
<Name>UserID</Name>
<PlaceHolder>username</PlaceHolder>
<ShowAsDescription>true</ShowAsDescription>
</RequiredParameter>
</RequiredParameters>
I don't want the values in the child tags, there is time where there will be more than one portal thus the need/want to use a list. I only need the values inside of the Name and ID tags. also if there is a blank ID tag I don't want to store the either one of them.
My current approach to this is not working as expected:
String URLString = "http://servername:8080/roambi/SourceManager";
XmlTextReader reader = new XmlTextReader(URLString);
List<Portal> lPortals = new List<Portal>();
String sPortal = "";
String sId = "";
while (reader.Read())
{
//Get Portal ID
if (reader.NodeType == XmlNodeType.Element && reader.Name == "Id")
{
reader.Read();
if (reader.NodeType == XmlNodeType.Text)
{
sId = reader.Value;
}
}
//Get Portal Name
if (reader.NodeType == XmlNodeType.Element && reader.Name == "Name")
{
reader.Read();
if (reader.NodeType == XmlNodeType.Text)
{
sPortal = reader.Value;
}
//Fill Portal List with Name and ID
if (sId != "" && sPortal != "")
{
lPortals.Add(new Portal
{
Portalname = sPortal,
Portalid = sId
});
}
}
}
foreach (Portal i in lPortals)
{
Console.WriteLine(i.Portalname + " " + i.Portalid);
}
See my standard class
class Portal
{
private String portalname;
private String portalid;
public String Portalname
{
get { return portalname; }
set { portalname = value; }
}
public String Portalid
{
get { return portalid; }
set { portalid = value; }
}
}
Please give me some advice and point me into a direction, As I said its been a while since I last programmed. My current Output is as follow:
Portal1 28577db1-956c-41f6-b775-a278c39e20a1
UserID user_name
UserID is in a child node and I do not want to display child nodes
It's much easier with XDocument class:
String URLString = "http://servername:8080/roambi/SourceManager";
XmlTextReader reader = new XmlTextReader(URLString);
XDocument doc = XDocument.Load(reader);
// assuming there's some root-node whose children are Source nodes
var portals = doc.Root
.Elements("Source")
.Select(source => new Portal
{
Portalname = (string) source.Element("Name"),
Portalid = (string) source.Element("Id")
})
.Where(p => p.Portalid != "")
.ToList();
For each <Source> node in your XML, code above will select direct children nodes (<Name> and <Id>) and build appropriate Portal instances.

Add to an arraylist from different arraylist index

I have two array lists one in which I read in values from an XML, and then I add a specific tag to a listbox. From the listbox I transfer over the tag to another listbox, but the problem I am having is when trying to get the values of the selected item in the listbox in array1 to move over to array2.
How can I do this and make sure all things saved in the current index of arraylist1 move to arraylist2?
//Initialisation
int moduleCount = 0;
bool isFull = false;
ArrayList chosen= new ArrayList();
ArrayList module = new ArrayList();
String name;
String code;
String info;
String semester;
String tSlot;
String lSlot;
String preReq;
string xmlDirectory = Directory.GetCurrentDirectory();
public Form1()
{
InitializeComponent();
createBox();
//List<Array> a=new List<Array>();
// Console.WriteLine(module.ToString());
// getXML();
}
private void createBox()
{
String workingDir = Directory.GetCurrentDirectory();
XmlTextReader textReader = new XmlTextReader(workingDir + #"\XML.xml");
textReader.Read();
XmlNodeType type;
while (textReader.Read())
{
textReader.MoveToElement();
type = textReader.NodeType;
if (type == XmlNodeType.Element)
{
if (textReader.Name == "Code")
{
textReader.Read();
code = textReader.Value;
Console.WriteLine(code);
}
if (textReader.Name == "Name")
{
textReader.Read();
name = textReader.Value;
//selectionBox.Items.Add(name);
Console.WriteLine(name);
}
if (textReader.Name == "Semester")
{
textReader.Read();
semester = textReader.Value;
Console.WriteLine(semester);
}
if (textReader.Name == "Prerequisite")
{
textReader.Read();
preReq = textReader.Value;
Console.WriteLine(code);
}
if (textReader.Name == "LectureSlot")
{
textReader.Read();
lSlot = textReader.Value;
Console.WriteLine(lSlot);
}
if (textReader.Name == "TutorialSlot")
{
textReader.Read();
tSlot = textReader.Value;
Console.WriteLine(tSlot);
}
if (textReader.Name == "Info")
{
textReader.Read();
info = textReader.Value;
Console.WriteLine(info);
module.Add(new Modules(code, name, semester, tSlot, lSlot, info, preReq));
}
}
//Console.WriteLine(module);
}
foreach (object o in module)
{
Modules m = (Modules)o;
//String hold = m.mName;
selectionBox.Items.Add(m.mName);
}
textReader.Close();
//button event handler to move from one listbox to the other
if (selectionBox.SelectedItem != null)
{
chosenBox.Items.Add(selectionBox.SelectedItem);
selectionBox.Items.Remove(selectionBox.SelectedItem);
chosen.Add(selectionBox.SelectedItem);
errorLabel.Text = "";
moduleCount++;
if (moduleCount >= 8)
{
isFull = true;
errorLabel.Text = "You have selected 8 Modules please fill the fields and submit";
selectionBox.Enabled = false;
}
}
else
{
errorLabel.Text = "Please select a module";
}
numberChosen.Text = String.Format(moduleCount.ToString());
//Write to XML
foreach (object o in chosen)
{
Modules m = (Modules)o;
if (m.mPreReq != "None")
{
MessageBox.Show("You must chose module " + m.mPreReq);
//errorLabel.Text = "You must chose module " + m.mPreReq;
errorLabel.Text = "There is a prereq course";
req = true;
}
}
Ok it seems like you need to relate m.mName that you add to selectionBox to the actual Module you added to the module ArrayList so that you can add the members of that module to the chosen ArrayList later on. Something like:
if (selectionBox.SelectedItem != null)
{
chosenBox.Items.Add(selectionBox.SelectedItem);
selectionBox.Items.Remove(selectionBox.SelectedItem);
foreach (Module m in module)
{
if (m.Name.Equals(selectionBox.SelectedItem)
{
chosen.Add(m.Info);
chosen.Add(m.Code);
...
break;
}
}
errorLabel.Text = "";
moduleCount++;
if (moduleCount >= 8)
{
isFull = true;
errorLabel.Text = "You have selected 8 Modules please fill the fields and submit";
selectionBox.Enabled = false;
}
else
{
errorLabel.Text = "Please select a module";
}
}
numberChosen.Text = String.Format(moduleCount.ToString());

Reading XML in C#

I have the following xml
<Objects>
<Object>
<ViewAngle>90</ViewAngle>
<ViewMode>ThirdPerson</ViewMode>
<Top>50</Top>
<Left>100</Left>
</Object>
</Objects>
I have the following code to read this xml
XmlDataDocument doc = new XmlDataDocument();
doc.Load(xmlPath);
XmlElement root = doc.DocumentElement;
XmlNodeList nodes = root.SelectNodes("/Objects/Object");
foreach (XmlNode node in nodes)
{
if (node.InnerXml.Contains("View"))
{
string viewType=node["View"].InnerText;
//..... other stuffs
}
if (node.InnerXml.Contains("ViewAngle"))
{
string viewAngle=node["ViewAngle"].InnerText;
//..... other stuffs
}
if (node.InnerXml.Contains("ViewMode"))
{
string viewMode=node["ViewMode"].InnerText;
//..... other stuffs
}
}
Above code is working fine if my xml contains one more tag i.e. <View>3dView</View>. But if it does not contains the View tag, then it produces error.
The InnerXml.Contains() is a simple string method that checks for the sub-string is present in the string or not.
In my case the Contains("View") returns true as View is present in ViewAngle and in ViewMode that is why it is going inside the if block of View but when it tries to read the node["View"], it produces error as View node is not exists in the xml.
How to resolve this error?
You are missing the fact that XML is structured data, not string. This is completely the wrong approach:
if (node.InnerXml.Contains("View")) {
// ...
}
You want this:
XmlNode child = node.SelectSingleNode("./View");
if (child != null) {
// now do something with 'child' ...
}
Try this loop instead:
foreach (XmlNode node in nodes) {
foreach (XmlNode prop in node.ChildNodes) {
if (prop.NodeType != XmlNodeType.Element)
continue;
switch (prop.Name) {
case "View":
string viewType = prop.InnerText;
// ...
break;
case "ViewAngle":
string viewAngle = prop.InnerText;
// ...
break;
case "ViewMode":
string viewMode = prop.InnerText;
// ...
break;
}
}
}
If the View element is not present, it cannot be found. You can use the GetElementsByTagName("View") method instead. This will return null if the view element is not there.
You can make some modifications in this function and use it acordingly
XDocument xDoc=XDocument.Load(#"C:\File.xml");
Process(xDoc.Root);
public static void Process(XElement element)
{
if (element.HasElements)
{
Console.WriteLine("<" + element.Name.LocalName + ">");
foreach (XElement child in element.Elements())
{
Process(child);
}
Console.WriteLine("<" + element.Name.LocalName + ">");
}
else
{
Console.WriteLine("<" + element.Name.LocalName);
if(element.HasAttributes)
{
foreach (XAttribute attr in element.Attributes())
{
Console.WriteLine(attr.Name +"="+attr.Value);
}
}
Console.WriteLine(">" + element.Value + "</" + element.Name.LocalName + ">");
}
}
if trageting 3.5 or above u can use linq
XDocument XDoc = XDocument.Load(#"C:\UrXMLFile.xml");
var q = from b in xml.Descendants("product")
select new
{
View = (string)b.Element("View") ?? "3dView",
ViewAngle = (double?)b.Element("ViewAngle") ?? 90.0,
ViewMode = (string)b.Element("ViewMode") ?? ThirdPerson,
Top = (double?)b.Element("Top") ?? 0.0,
Left = (double?)b.Element("Left") ?? 0.0
};
So if View is not found it have a default value always. Also u can create ur class object in select new. Note: I have not compiled it.

Categories