Convert Foreach Loop into Linq in C# - 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
}
}

Related

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.

How to set a value in XML using C#?

How to change the value of sourcePatientInfo in the following xml file using c#.
I can able to read the value using,
var elem = (from n in xml.Descendants("Slot")
where n.Attribute("name").Value == "sourcePatientInfo"
select n).FirstOrDefault();
How to change the same using C#?
<?xml version="1.0" encoding="utf-8"?>
<rs:SubmitObjectsRequest xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:rs="urn:oasis:names:tc:ebxml-regrep:registry:xsd:2.1" xmlns:rim="urn:oasis:names:tc:ebxml-regrep:rim:xsd:2.1" xmlns="urn:oasis:names:tc:ebxml-regrep:rim:xsd:2.1">
<LeafRegistryObjectList>
<ObjectRef id="urn:uuid:93606bcf-9494-43ec-9b4e-a7748d1a838d" />
<ExtrinsicObject id="Document01" mimeType="application/dicom" objectType="urn:uuid:7edca82f-054d-47f2-a032-9b2a5b5186c1">
<Name>
<LocalizedString value="Physical" />
</Name>
<Description />
<Slot name="sourcePatientId">
<ValueList>
<Value>pid1^^^&1.2.3&ISO</Value>
</ValueList>
</Slot>
<Slot name="sourcePatientInfo">
<ValueList>
<Value>PID-3|pid1^^^&1.2.3&ISO</Value>
<Value>PID-5|Doe^John^^^</Value>
<Value>PID-7|19560527</Value>
<Value>PID-8|M</Value>
<Value>PID-11|100 Main St^^Metropolis^Il^44130^USA</Value>
</ValueList>
</Slot>
I would like to change the values using c#. Am not able to figure out the way. Any Help to resolve this issue will be appreciated.
I want to change the
<Slot name="sourcePatientInfo">
<ValueList>
<Value>PID-3|pid1^^^&1.2.3&ISO</Value>
<Value>PID-5|Doe^John^^^</Value>
to the following value
<Slot name="sourcePatientInfo">
<ValueList> <Value>PID-3|MyPID</Value>
<Value>PID-5|MyName</Value>
I have also tried the following the code,
XmlNamespaceManager namespaceManager = new XmlNamespaceManager(xmlDoc1.NameTable);
namespaceManager.AddNamespace("rs", "urn:oasis:names:tc:ebxml-regrep:registry:xsd:2.1");
var query = "/rs:SubmitObjectsRequest/LeafRegistryObjectList/ExtrinsicObject";
XmlNodeList nodeList = xmlDoc1.SelectNodes(query, namespaceManager);
foreach (XmlNode node1 in nodeList)
{
if (node1.Attributes["Slot"].Value == "sourcePatientInfo")
{
node1.Attributes["ValueList"].Value = "Myvalue";
}
}
In this code, nodelist.count is always zero :-(. Kindly help me to resolve the issue.
If you need to update first two values:
var slot = xml.Descendants("Slot")
.Where(n => n.Attribute("name").Value == "sourcePatientInfo")
.FirstOrDefault();
if(slot == null)
{
throw new WhateverAppropriateHereEcxeption();
}
var values = slot.Descendants("Value").ToList();
if(values.Count < 2)
{
throw new WhateverAppropriateHereEcxeption();
}
values[0].Value = "PID-3|MyPID" // updating the first value
values[1].Value = "PID-5|MyName" // updating the second value
if you have to search by value you can do:
bool UpdateValue(XElement slot, string oldValue, string newValue)
{
var elem = slot.Descendants("Slot")
.Where(n => n.Name == "Value" && n.Value == oldValue)
.FirstOrDefault();
if(elem != null)
{
elem = newValue;
return true;
}
return false;
}
and inside some function
var slot = xml.Descendants("Slot")
.Where(n => n.Attribute("name").Value == "sourcePatientInfo")
.FirstOrDefault();
if(slot == null)
{
throw new WhateverAppropriateHereEcxeption();
}
UpdateValue(slot, "PID-3|pid1^^^&1.2.3&ISO", "PID-3|MyPID");
UpdateValue(slot, "PID-5|Doe^John^^^", "PID-5|MyName");
UPD when you call xml.Descendants("Slot") xml look only for elements in default namespace. I use an extension method as a quick workaround to avoid that:
public static IEnumerable<XElement> NsDescendants(this XContainer e, string elementName)
{
return e.Descendants().Where(d => d.Name.LocalName == elementName);
}
Finally my problem is solved with the following code.
XmlDocument xmlDocSOR = new XmlDocument();
XmlDocSOR.Load("filename.xml");
XmlNamespaceManager namespaceManager = new XmlNamespaceManager(xmlDocSOR.NameTable);
namespaceManager.AddNamespace("rs", "urn:oasis:names:tc:ebxml-regrep:registry:xsd:2.1");
namespaceManager.AddNamespace("ns", "urn:oasis:names:tc:ebxml-regrep:rim:xsd:2.1");
var query = "/rs:SubmitObjectsRequest/ns:LeafRegistryObjectList/ns:ExtrinsicObject/ns:Slot";
XmlNodeList nodeList = xmlDocSOR.SelectNodes(query, namespaceManager);
foreach (XmlNode plainnode in nodeList)
{
if (plainnode.Attributes["name"].Value == "sourcePatientId")
{
XmlNode childnode = plainnode.LastChild;
XmlElement ee1 = (XmlElement)childnode.FirstChild;
ee1.InnerText = sPatientID;
}
}
xmlDocSOR.Save("filename.xml");

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);
});

Add items if equal to (XML feed reading)

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.

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