Add items if equal to (XML feed reading) - c#

i am reading an XML feed and adding results like this:
XElement _xml;
try
{
if (!e.Cancelled)
{
_xml = XElement.Parse(e.Result);
Results.Items.Clear();
foreach (XElement value in _xml.Elements("runner").Elements("rr_event"))
{
FeedItem _item = new FeedItem();
_item.Title = value.Element("title").Value;
_item.Description = Regex.Replace(value.Element("description").Value,
#"<(.|\n)*?>", String.Empty);
_item.Sector = value.Element("sector").Value;
Results.Items.Add(_item);
}
}
}
My aim is to only add to Results if the XML tag "Sector" text equals to "A"
Many thanks. Dan

Totally agree with vortex, just add if("A".equals(_item.Sector)).
XElement _xml;
try
{
if (!e.Cancelled)
{
_xml = XElement.Parse(e.Result);
Results.Items.Clear();
foreach (XElement value in _xml.Elements("runner").Elements("rr_event"))
{
FeedItem _item = new FeedItem();
_item.Title = value.Element("title").Value;
_item.Description = Regex.Replace(value.Element("description").Value,
#"<(.|\n)*?>", String.Empty);
_item.Sector = value.Element("sector").Value;
if("A".equals(_item.Sector)) Results.Items.Add(_item);
}
}
}
This should do it.

Related

How can I convert an HtmlAttribute to a string?

var web = new HtmlWeb();
var doc = web.Load(page);
var Articles = doc.DocumentNode.SelectNodes("//*[#class = 'b-product-grid-tile js-tile-container']");
var href = doc.DocumentNode.SelectNodes("//a[#href]");
foreach (HtmlNode link in href)
{
HtmlAttribute att = link.Attributes["href"];
_entries.Add(new EntryModel { Link = att });
// att.ToString(); <----- Want to convert the HtmlAttribute to a string.
}
Full Code of my Scraper:
EntryModel List:
Main Window:
The links I need:
The easiest would be using the System.Web.Mvc.TagBuilder class:
var attributes = new { #class = "myClass", id = "elId" };
var tag = new TagBuilder("href");
tag.MergeAttributes(new RouteValueDictionary(attributes));
return tag.ToString();

How to hold more than one values in XML element?

xWaarde.Value is overriding with new values and flushing old values.
how to stored multiple values in Xelement.
private XElement[] AddCoordinatenList(XElement childElements)
{
XmlNode xmlCoordinatenNode = Utility.GetMappingValues(Constants.BMCoordinaten, ConnectionType.BM);
XmlNode xWaardeNode = Utility.GetMappingValues(Constants.BMXwaarde, ConnectionType.BM);
XmlNode yWaardeNode = Utility.GetMappingValues(Constants.BMYwaarde, ConnectionType.BM);
XmlNode CoordinateX = Utility.GetMappingValues(Constants.XCoordinate, ConnectionType.BM);
XmlNode CoordinateY = Utility.GetMappingValues(Constants.YCoordinate, ConnectionType.BM);
var coordinatenList = from document in childElements.DescendantsAndSelf() where document.Name.LocalName == xmlCoordinatenNode.Name select document;
List<XElement> xcoordinatenList = new List<XElement>();
XElement xWaarde = new XElement(CoordinateX.Name);
XElement yWaarde = new XElement(CoordinateY.Name);
if (coordinatenList.Count() > 0)
{
foreach (XElement element in coordinatenList.Descendants())
{
if (element.Name.LocalName == xWaardeNode.Name)
{
xWaarde.Value = element.Value;
}
if (element.Name.LocalName == yWaardeNode.Name)
{
yWaarde.Value = element.Value;
}
}
}
return xcoordinatenList.ToArray();
}
It's not possible. Use Attributes:
XAttribute attribute = new XAttribute("name1", <value>);
element.Add(attribute);
attribute = new XAttribute("name2", <value>);
element.Add(attribute);
You can also add a list of child elements or add all strings as one string, separated by a comma for instance.

Convert Foreach Loop into Linq in C#

Folks, I want to convert these loops into simple loop.
I saw answers related to linq but couldn't make out from that. IS there any other possible ways for this snippet?
XmlDocument manifestXmlFile = new XmlDocument();
manifestXmlFile.Load(manifestFileName);
foreach (XmlNode rules in manifestXmlFile.DocumentElement.ChildNodes)
{
foreach (XmlNode ruleNode in rules)
{
foreach (XmlNode childNodeAttributes in ruleNode)
{
foreach (XmlNode childNodeAttrib in childNodeAttributes.ChildNodes)
{
XmlElement ruleElement = (XmlElement)ruleNode;
foreach (XmlNode childNodeConditions in childNodeAttrib.ChildNodes)
{
foreach (XmlNode childNodeCond in childNodeConditions.ChildNodes)
{
if (childNodeCond.Name.ToUpper() == "CONDITION")
{
if (childNodeCond.Attributes["type"].Value.ToUpper() == "HEALTHY")
{
string ruleId = ruleElement.Attributes["ruleid"].Value;
string attributeName = childNodeAttrib.Attributes["name"].Value;
string attributeType = childNodeAttrib.Attributes["type"].Value;
string condTypeValue = childNodeCond.Attributes["type"].Value;
string operatorValue = childNodeCond.Attributes["operator"].Value;
string healthyConditionValue = childNodeCond.FirstChild.InnerText;
var guid = new Guid(ruleId);
//Conversion of enum types
PsmsOperator psmsOperator = (PsmsOperator)Enum.Parse(typeof(PsmsOperator), operatorValue, true);
TypeCode psmsAttributeType = (TypeCode)Enum.Parse(typeof(TypeCode), attributeType, true);
Rule rule = new Rule(guid, attributeName, healthyConditionValue, psmsOperator);
Rule(attributes, guid);
}
}
}
}
}
}
}
}
Is there any other better way than this or linq?
Use XPath to select the node you want.
foreach (var node in manifestXmlFile.SelectNodes(#"//condition[#type = 'healthy']")
{
...
}
Well.. It can be done just because you can, but its ugly :)
(Credits goes to R#):
foreach (Rule rule in from XmlNode rules in manifestXmlFile.DocumentElement.ChildNodes
from XmlNode ruleNode in rules
from XmlNode childNodeAttributes in ruleNode
from XmlNode childNodeAttrib in childNodeAttributes.ChildNodes
let ruleElement = (XmlElement) ruleNode
from XmlNode childNodeConditions in childNodeAttrib.ChildNodes
from XmlNode childNodeCond in childNodeConditions.ChildNodes
where childNodeCond.Name.ToUpper() == "CONDITION"
where childNodeCond.Attributes["type"].Value.ToUpper() == "HEALTHY"
let ruleId = ruleElement.Attributes["ruleid"].Value
let attributeName = childNodeAttrib.Attributes["name"].Value
let attributeType = childNodeAttrib.Attributes["type"].Value
let condTypeValue = childNodeCond.Attributes["type"].Value
let operatorValue = childNodeCond.Attributes["operator"].Value
let healthyConditionValue = childNodeCond.FirstChild.InnerText
let guid = new Guid(ruleId)
let psmsOperator = (PsmsOperator) Enum.Parse(typeof (PsmsOperator), operatorValue, true)
let psmsAttributeType = (TypeCode) Enum.Parse(typeof (TypeCode), attributeType, true)
select new Rule(guid, attributeName, healthyConditionValue, psmsOperator))
{
//do something with rule varialbe
}
May be this will do the trick
XElement root = XElement.Load(manifestFileName);
root.Element("RootElement").Elements("rules").Elements("ruleNode").Elements("CONDITION").All<XElement>(xe =>
{
if(xe.Attribute("type").Value.ToUpper() == "HEALTHY")
{
//do
}
}

Child Nodes in XML File

I am trying to get the child nodes in my xml file and print it out..however i cannot. It is just printing one element instead of all. please help.
XDocument doc = XDocument.Load(GlobalClass.GlobalUrl);
XElement xelement = XElement.Load(GlobalClass.GlobalUrl);
var query = from nm in xelement.Elements("EmployeeFinance")
where (int)nm.Element("EmpPersonal_Id") == empID
select new AllowancePaid
{
Income_ID = (decimal)nm.Element("Allowance-Grade")
.Element("Amount")
};
var x = query.ToList();
foreach (var ele in x) {
Debug.WriteLine(ele.Income_ID);
}
My XML File
<EmployeeFinance>
<EmployeeEmploy_Id>4891</EmployeeEmploy_Id>
<EmpPersonal_Id>28407</EmpPersonal_Id>
<Employee_Number>11715</Employee_Number>
<Allowance-Grade>
<Amount BenListID="32">6000.00</Amount>
<Amount BenListID="59">100000.00</Amount>
</Allowance-Grade>
</EmployeeFinance>
Adding the elements
var result = from element in doc.Descendants("EmployeeFinance")
where int.Parse(element.Element("EmpPersonal_Id").Value) == tt.Employee_Personal_InfoEmp_id
select element;
foreach (var ele in result)
{
ele.Element("Allowance-Grade")
.Add(new XElement("Amount", new XAttribute("BenListID", tt.ID), tt.Amount));
doc.Save(GlobalClass.GlobalUrl);
}
problem is you selecting one amount node .Element("Amount") but there are many nodes. Change it to .Elements("Amount") and need to change few places. I haven't tested but logic should something like below will work
`
var query = from nm in xelement.Elements("EmployeeFinance")
where (int)nm.Element("EmpPersonal_Id") == empID
select new AllowancePaid
{
AllowanceList = nm.Element("Allowance-Grade").Elements("Amount").Select( a=>(decimal)a.Value).ToList()
};
foreach (var ele in query) {
foreach (var a in ele.AllowanceList) {
Debug.WriteLine(a);
}
}
XDocument xelement = XDocument.Parse(xml);
var query = from nm in xelement.Elements("EmployeeFinance")
where (int)nm.Element("EmpPersonal_Id") == 28407
select new
{
Amounts = nm.Element("Allowance-Grade")
.Elements("Amount")
.Select(
row =>
new {
id = row.Value
}
).ToList()
};
var x = query.ToList();
foreach (var ele in x)
{
foreach(var a in ele.Amounts)
Console.WriteLine(a);
}

Foreach loop XmlNodeList

Currently I have the following code:
XmlDocument xDoc = new XmlDocument();
xDoc.Load("http://api.twitter.com/1/statuses/user_timeline.xml?screen_name=twitter");
XmlNodeList tweets = xDoc.GetElementsByTagName("text");
foreach (int i in tweets)
{
if (tweets[i].InnerText.Length > 0)
{
MessageBox.Show(tweets[i].InnerText);
}
}
Which doesn't work, it gives me System.InvalidCastException on the foreach line.
The following code works perfectly (no foreach, the i is replaced with a zero):
XmlDocument xDoc = new XmlDocument();
xDoc.Load("http://api.twitter.com/1/statuses/user_timeline.xml?screen_name=twitter");
XmlNodeList tweets = xDoc.GetElementsByTagName("text");
if (tweets[0].InnerText.Length > 0)
{
MessageBox.Show(tweets[0].InnerText);
}
I know that there is already a marked answer, but you can do it like you did in your first try, you just need to replace the int with XmlNode
XmlDocument xDoc = new XmlDocument();
xDoc.Load("http://api.twitter.com/1/statuses/user_timeline.xml?screen_name=twitter");
XmlNodeList tweets = xDoc.GetElementsByTagName("text");
foreach (XmlNode i in tweets)
{
if (i.InnerText.Length > 0)
{
MessageBox.Show(i.InnerText);
}
}
tweets is a node list. I think that what you're trying to do is this:
XmlDocument xDoc = new XmlDocument();
xDoc.Load("http://api.twitter.com/1/statuses/user_timeline.xml?screen_name=twitter");
XmlNodeList tweets = xDoc.GetElementsByTagName("text");
for (int i = 0; i < tweets.Count; i++)
{
if (tweets[i].InnerText.Length > 0)
{
MessageBox.Show(tweets[i].InnerText);
}
}
It is not of Int type, That is the reason you are getting a casting exception. You can either replace int with the appropriate type or simply make use of type inference (implicitly typed variables) to handle this. Here i am using typeinference.by saying type as var, The compiler will understand it is of type of the iterator variable in tweets collection
foreach (var i in tweets)
{
if (i!=null)
{
string tweet= (((System.Xml.XmlElement)(i))).InnerText;
MessageBox.Show(tweet);
}
}
EDIT : With the Wonderful LINQtoXML, Your code can be rewritten like this.
string url = "http://api.twitter.com/1/statuses/user_timeline.xml?screen_name=twitter";
XElement elm = XElement.Load(url);
if (elm != null)
{
foreach (var status in elm.Elements("status"))
{
string tweet = status.Element("text").Value;
MessageBox.Show(ss);
}
}
All the answers seem to be a bit outdated Imperative examples so I will add a declarative one. This is not doing what the OP wanted but I'm sure you'll get the point.
public static List<System.Xml.XmlNode> toList(System.Xml.XmlNodeList nodelist){
List<System.Xml.XmlNode> nodes = new List<System.Xml.XmlNode>();
foreach (System.Xml.XmlNode node in nodelist)
{
nodes.Add(node);
}
return nodes;
}
public static ReadMeObject setXml(ReadMeObject readmeObject){
readmeObject.xmlDocument = new System.Xml.XmlDocument();
readmeObject.xmlDocument.LoadXml("<body>"+readmeObject.htmlStringContent+"</body>");
System.Xml.XmlNodeList images = readmeObject.xmlDocument.SelectNodes("//img");
Array.ForEach(
Functions.toList( images )
.Where((image) => image.Attributes != null)
.Where((image) => image.Attributes["src"] != null)
.Where((image) => image.Attributes["src"].Value != "")
.ToArray()
, (image) => {
Console.WriteLine(image.Attributes["src"].Value);
}
);
return readmeObject;
}
foreach (XmlNode node in tweets)
{
if (tweets[i].InnerText.Length > 0)
{
MessageBox.Show(tweets[node].InnerText);
}
}
I've changed the 'I', which you cannot use, to XmlNode, which selects a single line of your list.
You can loop through the Collection with .GetEnumerator()
this code is taken Microsoft Documentation :
XmlNodeList elemList = root.GetElementsByTagName("title");
IEnumerator ienum = elemList.GetEnumerator();
while (ienum.MoveNext()) {
XmlNode title = (XmlNode) ienum.Current;
Console.WriteLine(title.InnerText);
}
Use this simple extension method to iterate through XmlNodeList:
public static void ForEachXml<TXmlNode>(this XmlNodeList nodeList, Action<TXmlNode> action)
{
foreach (TXmlNode node in nodeList) action(node);
}
Method Call:
xDoc.GetElementsByTagName("text").ForEachXML<XmlNode>(tweet =>
{
if (tweet.InnerText.Length > 0)
MessageBox.Show(tweet.InnerText);
});

Categories