c# "if" statement not working . Checking all variables are null - c#

I have this XML file
<?xml version="1.0" encoding="utf-8"?>
<message>
<success/>
<bookings>
<booking>
<rooms>
<room roomCode ="101" uniqueId="abc">
<stays>
<stay usedfrom="9:30" usedto="10:30" quantity="1" Price="62.5" rateCode="1"/>
</stays>
<extras>
<extra from="9:30" to="10:30" unitPrice="5.5" extraCode="coffee" quantity="1" inclusive="0"/>
</extras>
<guests>
<guest firstName="John" lastName="Doe" title="MR" ageRange="0"/>
</guests>
</room>
<room roomCode ="Brd" uniqueId="xyz">
<stays>
<stay usedfrom="13:30" usedto="15:30" quantity="1" unitPrice="60.0000" rateCode="RACK"/>
</stays>
<guests>
<guest firstName="Jean" lastName="Doe" title="MRS" ageRange="0"/>
</guests>
</room>
</rooms>
</booking>
and i'm trying to run a check to make sure its in the correct format (ie certain elements are present).
The code I have been using is
XmlNodeList Successful = doc.GetElementsByTagName("success");
XmlNodeList Bookings = doc.GetElementsByTagName("bookings");
XmlNodeList Booking = doc.GetElementsByTagName("booking");
XmlNodeList Rooms = doc.GetElementsByTagName("rooms");
if ((Successful != null) && (Bookings != null) && (Booking != null) && (Rooms != null))
{
//do something
}
else
{
//do something else
}
This ALWAYS works thought.
If I change the one of the values to
XmlNodeList Rooms = doc.GetElementsByTagName("NoSuchElement");
(which does not exist in the XML) it still "works".
Can someone point out what I've done wrong (I've tried removing the outer brackets from the "if" statement, but this did not change the outcome).
Thanks

As opf the docs:
An XmlNodeList containing a list of all matching nodes. If no nodes
match name, the returned collection will be empty.
The method can´t return null, but an empty list. So check for this instead:
if(theNodeList.Any()) { ... }
else { /* error */ }

This is because oc.GetElementsByTagName("NoSuchElement"); is never null.
As per the documentation, if no element exists, it returns en empty collection which is not null.
You have to check the Count of the result.
something like :
if (Successful.Count() != 0 && ...)
or
if (!Successful.Any() && ... )

Related

Cant delete XmlElement using Linq query

I have a very basic linq query to be able to delete one node from xml file.But when ı run this code I am getting this exception below.
Sequence contains no elements
then I have used FirstOrDefault() instead First() ( as mentioned earlier posts) and this time Exection message turned into this
Object reference not set to an instance of an object.
This my code
protected void Page_Load(object sender, EventArgs e)
{
XDocument doc = XDocument.Load(Server.MapPath("Kitaplar.xml"));
var toDelete = (from data in doc.Elements("Kitap") where data.Attribute("id").Value == "1" select data).FirstOrDefault();
toDelete.Remove();
doc.Save(Server.MapPath("Kitaplar.xml"));
}
And this is the xmlfile
<?xml version="1.0" encoding="utf-8"?>
<Kitaplar>
<Kitap id="1">
<Kitapadi>asasa</Kitapadi>
<Yazar>sasas</Yazar>
<Sayfa>22</Sayfa>
</Kitap>
<Kitap id="2">
<Kitapadi>jhjh</Kitapadi>
<Yazar>kjkj</Yazar>
<Sayfa>33</Sayfa>
</Kitap>
<Kitap id="3">
<Kitapadi>lkjhg</Kitapadi>
<Yazar>gffd</Yazar>
<Sayfa>988</Sayfa>
</Kitap>
<Kitap id="4">
<Kitapadi>lkjhg</Kitapadi>
<Yazar>gffd</Yazar>
<Sayfa>988</Sayfa>
</Kitap>
</Kitaplar>
Everyting looks ok to me.what am i doing wrong ?
I took the liberty of writing a method that takes the file, element and ID; then removes the Element accordingly.
private bool DeleteRowWithID(string fileName, string element, string id)
{
XDocument doc = XDocument.Load(fileName);
if (doc.Root == null)
return false;
XElement toRemove = doc.Root.Elements(element).Where(e => e.Attribute("id").Value == id).FirstOrDefault();
if (toRemove == null)
return false;
toRemove.Remove();
doc.Save(fileName);
return true;
}
Above method loads the XmlDocument in an XDocument (which allows LINQ to XML). It checks if the root isn't empty, then finds the element you specified.
It checks if the element exists; then removes that element from the document, and save the made removal.
Last, it returns true to indicate that the element has actually been removed.
If you just want the element and stick to your method, use the following:
XElement toRemove = doc.Root.Elements("Kitap").Where(e => e.Attribute("id").Value == "1").FirstOrDefault();

Check if any XML nodes exists with Specific Attribute using LINQ C#

This is my XML:
<configuration>
<Script name="Test Script">
<arguments>
<argument key="CheckStats" value="True" />
<argument key="ReferenceTimepoint" value="SCREENING" />
<argument key="outputResultSetName" value="ResultSet" />
</arguments>
</Script>
</configuration>
I am trying to use this linq statement to grab an argument element's value attrbiute if a specific key attribute exists.
XElement root = XElement.Load(configFileName);
var AttrVal = from el in root.Elements("Script").Elements("arguments").Elements("argument")
where el.Attribute("key").Value == "CheckStats"
select el.Attribute("value").Value;
Then I want to try and parse the attribute value into a boolean:
bool checkVal;
if (AttrVal != null)
{
if (!bool.TryParse(AttrVal.First().ToString(), out checkVal))
{
throw new Exception(string.Format("Invalid value"));
}
}
This code works if there is an element with that attribute, but if there isn't one, I get a System.InvalidOperationException: Sequence contains no elements.
How can I get around that?
I thought by checking if (AttrVal != null) it would work.
Should I replace that with if (AttrVal.FirstOrDefault() != null) or something similar?
Thanks
In if statement, you can write
if (AttrVal != null && AttrVal.Any())
EDIT: I'm wrong. The exception should come from First(), not any of Elements(). Old answer:
from el in root.Descendants("argument")
Or
from el in root.XPathSelectElements("./Script/arguments/argument")
you have to check if there is already your attribute in the element where el.Attributes("key")!=null&&
XElement root = XElement.Load("config.xml");
var AttrVal = from el in root.Elements("Script").Elements("arguments").Elements("argument")
where el.Attributes("key")!=null&& el.Attribute("key").Value == "CheckStats"
select el.Attribute("value").Value;
bool checkVal;
if (AttrVal != null)
{
if (!bool.TryParse(AttrVal.First().ToString(), out checkVal))
{
throw new Exception(string.Format("Invalid value"));
}
}
Here's a way to eliminate those pesky null checks - seek ahead with XPath to determine whether a node with both the necessary attributes (viz key="CheckStats" AND a value) exists, then parse it.
bool checkVal;
// using System.Xml.XPath;!
var el = root.XPathSelectElement(
"/Script/arguments/argument[#key='CheckStats' and #value]");
if (el != null && !bool.TryParse(el.Attribute("value").Value,
out checkVal))
{
throw new Exception(string.Format("Invalid value"));
}

Finding a specific xml element containt

Ihave a large xml file and I want to get child element value by giving the parent child element value, I am new in xml file please any help here is my xml:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<masterController> <uuid>XXXXXXXXXXXXXXXXXXXXXXXXX</uuid>
<channels>
<channel>
<nodeGroups>
<nodeGroup>
<analogNode>
<typeCode>8</typeCode>
<id>1</id>
<sdos>
<sdo>
<description>Host ID</description>
<compareLevel>Ignore</compareLevel>
<datafield xmlns:xsi="http://www.XXXXX.XXXX/XXXXX/XMLSchema-instance"
xsi:type="intField">
<description>Host ID</description>
<compareLevel>Ignore</compareLevel>
<offset>2</offset>
<size>1</size>
<readonly>true</readonly>
<isMappedToPdo>false</isMappedToPdo>
<ownerNodeSerial>12102904</ownerNodeSerial>
<ownerSdoIndex>3</ownerSdoIndex>
<data xsi:type="intData">
<value xmlns:xs="http://www.XX.CC/2XXX/XMLSchema" xsi:type="xs:int">2</value>
<unit></unit>
<min>1</min>
<max>15</max>
</data>
<intValue>2</intValue>
</datafield>
<index>3</index>
<totalbytes>3</totalbytes>
</sdo>
<sdo>
<description>Host ID</description>
<compareLevel>Ignore</compareLevel>
<datafield xmlns:xsi="http://www.XXXXX.XXXX/XXXXX/XMLSchema-instance"
xsi:type="intField">
<description>Host ID</description>
<compareLevel>Ignore</compareLevel>
<offset>2</offset>
<size>1</size>
<readonly>true</readonly>
<isMappedToPdo>false</isMappedToPdo>
<ownerNodeSerial>12102905</ownerNodeSerial>
<ownerSdoIndex>4</ownerSdoIndex>
<data xsi:type="intData">
<value xmlns:xs="http://www.XX.CC/2XXX/XMLSchema" xsi:type="xs:int">16</value>
<unit></unit>
<min>1</min>
<max>15</max>
</data>
<intValue>2</intValue>
</datafield>
<index>3</index>
<totalbytes>3</totalbytes>
</sdo>
</sdos>
</analogNode>
</nodeGroup>
</nodeGroups>
</channel> </channels> </masterController>
I' am trying this but am not geting anything:
XElement root = XElement.Load(Server.MapPath("sample.xml"));
IEnumerable<XElement> masterco = from el in root.Elements("sdo") where (from add in el.Elements("datafield")
where
(string)add.Element("ownerNodeSerial") == TextBox1.Text &&
(string)add.Element("ownerSdoIndex") == TextBox1.Text
select add)
.Any()
select el;
foreach (XElement el in masterco)
{
TextBox3.Text = (string)el.Element("value");
}
I want to get this:
<value xmlns:xs="http://www.XX.CC/2XXX/XMLSchema" xsi:type="xs:int">16</value>
and be able to update it.
There is one major error in your query:
You are using Elements on root, but you are looking for the tag sdo which is not a direct child of the root tag. You have to use Descendants instead.
Additionally, I think you want to have an OR instead of an AND regarding the text of TextBox1.
Fix it:
var masterco = from el in root.Descendants("sdo")
where (from add in el.Elements("datafield")
where
(string)add.Element("ownerNodeSerial") == TextBox1.Text ||
(string)add.Element("ownerSdoIndex") == TextBox1.Text
select add).Any()
select el;
To actually get the value you want, you should use a different query. There is really no need to select the sdo tag at all.
var value = root.Descendants("datafield")
.Where(x => (string)x.Element("ownerNodeSerial") == TextBox1.Text ||
(string)x.Element("ownerSdoIndex") == TextBox1.Text)
.Select(x => (string)x.Element("data").Element("value"))
.Single();
TextBox3.Text = value;
You can see that I am assuming that in the whole XML document only one matching datafield/data/value entry exists. I derive that information from the way you update your textbox. This would make no sense if there would be multiple tags - the values would overwrite each other in the text box.

Linq never finding element in XDocument

I have the following XDocument called XDoc:
<?xml version="1.0" encoding="utf-8"?>
<DatabaseList>
<Database DatabaseName="c2501_data">
<Plugin PluginName="FooPlugin" LastRun="1/21/2013 3:22:08 PM" />
<Plugin PluginName="SpecialPlugin" LastRun="2013-01-21T15:22:09.3791103-05:00" />
<Plugin PluginName="BarPlugin" LastRun="2013-01-21T15:23:13.0964814-05:00" />
</Database>
</DatabaseList>
I'm writing a program that searches to see when the last time a plugin was run on a database, if at all. I use the following two pieces of code to figure out if an entry exists for a plugin on a database:
var h = (from el in XDoc.Root.Elements("Database")
where el.Element("Plugin").Attribute("PluginName").Value=="FooPlugin"
&& el.Attribute("DatabaseName").Value=="c2501_data"
select el.Element("Plugin"));
var e = (from el in XDoc.Root.Elements("Database")
where el.Element("Plugin").Attribute("PluginName").Value=="BarPlugin"
&& el.Attribute("DatabaseName").Value == "c2501_data"
select el.Element("Plugin"));
if ((from el in XDoc.Root.Elements("Database")
where el.Element("Plugin").Attribute("PluginName").Value == "BarPlugin"
&& el.Attribute("DatabaseName").Value == "c2501_data"
select el.Element("Plugin")).Count() == 0)
{
XElement SpecialPlugin = new XElement("Plugin",
new XAttribute("PluginName", "BarPlugin"),
new XAttribute("LastRun", DateTime.Now));
var CurNode = from node in XDoc.Root.Elements("Database")
where (string)node.Attribute("DatabaseName").Value == "c2501_data"
select node;
foreach (var node in CurNode)
node.Add(SpecialPlugin);
XDoc.Save(RuntimesPath);
//XDoc.Root.Elements("Database").Attribute("DatabaseName").
}
The problem that I'm having is that even though there is clearly an entry for BarPlugin, the count will always return 0 and e will always be unable to create an enumberable. Can anyone explain to me why this might be? FooPlugin always works correctly and returns the Plugin information for h.
Thanks for any help.
You're selecting a Database element where it contains a child element called Plugin with a given name. Since you have only one Database element, you're getting the same outer element each time. You then take that database element and return the first Plugin child, which will always be Foo, in this case. You need to find the appropriate Database element and then query through each of the child elements so you can return them:
public static XElement GetPlugin(XDocument XDoc, string databaseName, string pluginName)
{
var h = from database in XDoc.Root.Elements("Database")
where database.Attribute("DatabaseName").Value == databaseName
from plugin in database.Elements("Plugin")
where plugin.Attribute("PluginName").Value == pluginName
select plugin;
return h.FirstOrDefault();
}
Or, if you prefer, in method syntax:
var q = XDoc.Root.Elements("Database")
.Where(db => db.Attribute("DatabaseName").Value == databaseName)
.SelectMany(db => db.Elements("Plugin"))
.Where(plugin => plugin.Attribute("PluginName").Value == pluginName);
return q.FirstOrDefault();
Try this:
var db = XDoc.Root.Elements("Database");
var z = (from el in db.Elements("Plugin")
where el.Attribute("PluginName").Value == "BarPlugin"
&& el.Parent.Attribute("DatabaseName").Value == "c2501_data"
select el).FirstOrDefault();
if(z != null)
.....
I'm using Elements() method, to get all child elements and Parent property to look for the parent element "DatabaseName".
Problem in your code is that your el.Element() is searching only for the first element, thus it can find only "FooPlugin", which is on the first position in the xml.
From MSDN doc Element():
Gets the first (in document order) child element with the specified XName.

How to get the value of a node inside a large XDocument

Been working a lot with Google Checkout and the responses are always huge amount of XML data. What i want is to parse out the node called merchant-private-data from this rubble.
It might not always be present and it might be on several positions (with the same value).
I tried a simple approach but that did not yield any results.
From the sandbox response:
<?xml version="1.0" encoding="UTF-8"?>
<authorization-amount-notification xmlns="http://checkout.google.com/schema/2" serial-number="123456789">
<authorization-amount currency="USD">11.65</authorization-amount>
<authorization-expiration-date>2012-08-21T12:30:16.000Z</authorization-expiration-date>
<avs-response>Y</avs-response>
<cvn-response>U</cvn-response>
...
<order-summary>
...
<shopping-cart>
...
<merchant-private-data>
<MERCHANT_DATA_HIDDEN>50c77071-aeea-40fe-962b-f4d51d1f9b0</MERCHANT_DATA_HIDDEN>
</merchant-private-data>
...
</shopping-cart>
...
</order-summary>
</authorization-amount-notification>
Might be in more locations and might not always be in that location.
My idea was this, but it did not work (yields 0 results even though i can see the node in the xml):
XDocument input = XDocument.Parse(xmldata);
string privateData = null;
var privateDataNode = (from nodes in input.Descendants("merchant-private-data") select nodes).FirstOrDefault();
if (privateDataNode != null && privateDataNode.HasElements && privateDataNode.Element("MERCHANT_DATA_HIDDEN") != null)
privateData = privateDataNode.Element("MERCHANT_DATA_HIDDEN").Value;
You're missing namespace, try it like this:
XDocument input = XDocument.Parse(xmldata);
XNamespace ns = input.Root.Name.Namespace;
string privateData = null;
var privateDataNode = (from nodes in input.Descendants(ns + "merchant-private-data") select nodes).FirstOrDefault();
if (privateDataNode != null && privateDataNode.HasElements && privateDataNode.Element(ns + "MERCHANT_DATA_HIDDEN") != null)
privateData = privateDataNode.Element(ns + "MERCHANT_DATA_HIDDEN").Value;

Categories

Resources