This query seems to be valid, but I have 0 results.
IEnumerable<XElement> users =
(from el in XMLDoc.Elements("Users")
where (string)el.Attribute("GUID") == userGUID.ToString()
select el);
My XML is as follows:
<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<Users>
<User GUID="68327fe2-d6f0-403b-a7b6-51860fbf0b2f">
<Key ID="F7000012ECEAD101">
...
</Key>
</User>
</Users>
Do you have any clues to shed some light onto this?
Well, the Users elements don't have GUID attributes. Two suggested options:
Look at XDocument.Root.Elements("User")
Use Descendants("User") to find all User elements.
I'll stick with the former for the moment. That gives us:
IEnumerable<XElement> users =
(from el in XMLDoc.Root.Elements("User")
where (string) el.Attribute("GUID") == userGUID.ToString()
select el);
Now, we can still tidy this up further. Firstly, let's cast to Guid instead of string:
IEnumerable<XElement> users =
(from el in XMLDoc.Root.Elements("User")
where (Guid) el.Attribute("GUID") == userGUID
select el);
However there's not a lot of reason to use a query expression here... all you're applying is a single predicate. Let's just use the Where method directly:
IEnumerable<XElement> users =
XMLDoc.Root
.Elements("User")
.Where(el => (Guid) el.Attribute("GUID") == userGUID);
How you lay it out is up to you, of course :) With a longer line, you can probably align everything up under a longer first line:
IEnumerable<XElement> users = XMLDoc.Root
. etc
Now, finally - what about if the User element doesn't have a GUID attribute? Currently, this code will throw an exception. That may be exactly what you want - or it may not. If it's not, you can make it ignore such things by casting to Nullable<Guid> aka Guid? instead:
IEnumerable<XElement> users =
XMLDoc.Root
.Elements("User")
.Where(el => (Guid?) el.Attribute("GUID") == userGUID);
change Users in the 2nd line to User. Like this:
IEnumerable<XElement> users = (from el in XMLDoc.Root.Elements("User")
where (string)el.Attribute("GUID") == userGUID.ToString()
select el);
I'm assuming XMLDoc is an XDocument, and not the root element itself.
Related
I have the following chunk of XML code that I can easily generate.
<?xml version="1.0" encoding="utf-8"?>
<sessions>
<session date="14.10.2016" time="0:1" amount="1">
<Folder>C:\Users</Folder>
<Folder>C:\Tes2t</Folder>
<Folder>C:\Asgbsf\Aleksei</Folder>
</session>
<session date="14.10.2016" time="15:40" amount="7">
<Folder>C:\Users</Folder>
<Folder>C:\Tes2taaaa</Folder>
<Folder>C:\Asgbsf\Aleksei</Folder>
</session>
</sessions>
I am searching for data with attribute time 15:40 and date 14.10.2016 using following function
private static IEnumerable<XElement> FindElements(string filename, string date, string time)
{
XElement x = XElement.Load(filename);
return x.Descendants().Where(e => e.Attributes("date").Any(a => a.Value.Equals(date)) &&
e.Attributes("time").Any(a => a.Value.Equals(time)));
}
Function being executed like:
foreach (XElement x in FindElements(pathToXml, "14.10.2016", "15:40"))
Console.WriteLine(x.ToString());
Everything is fine, but the output is
<session date="14.10.2016" time="15:40" amount="7">
<Folder>C:\Users</Folder>
<Folder>C:\Tes2taaaa</Folder>
<Folder>C:\Asgbsf\Aleksei</Folder>
</session>
And I need just the folders, eg.
<Folder>C:\Users</Folder>
<Folder>C:\Tes2taaaa</Folder>
<Folder>C:\Asgbsf\Aleksei</Folder>
How do I achieve this? Help please.
(It seems that I am a little bit late, but..) in some cases like this using Xpath is easier than Linq .
var folders = XDocument.Load(filename)
.XPathSelectElements("//session[#date='14.10.2016' and #time='15:40']/Folder");
You are currently returning the Element that has attribute of date and time with these values. What you should add to it is to return its child elements of Folder. You can do this by adding .Elements("Folder") after the .Where.
However, I think you can write your query a bit nicer:
Look for all the sessions that the values of those attriute equal to the given input. Then return the element.Elements("Folder").
I've added the .SelectMany() to flatten the inner list of child elements
string date = "14.10.2016";
string time = "15:40";
var result = (from element in XDocument.Load("data.xml").Descendants("session")
where element.Attribute("date")?.Value == date &&
element.Attribute("time")?.Value == time
select element.Elements("Folder")).SelectMany(item => item).ToList();
I have the following XML:
<?xml version="1.0" encoding="UTF-8"?>
<game>
<name>Space Blaster</name>
<description></description>
<version>1</version>
<fullscreen>false</fullscreen>
<width>640</width>
<height>640</height>
<c2release>6900</c2release>
</game>
It's guaranteed to only have 1 game record in it. I'd like to retrieve each property value, this is what I've tried:
string ArcadeXMLLocation = Settings.GamePergatoryLocation + UserID + "/unzipped/arcade.xml";
XDocument loaded = XDocument.Load(ArcadeXMLLocation);
var q = (from c in loaded.Descendants("game") select (string)c.Element("name")).SingleOrDefault();
Response.Write(q.name);
But I can't seem to take any values like this (intellisense hates it!) Can someone show me how it's done?
I think you just need to get the value of the element:
string val = doc.Descendants("game")
.Select(x => x.Element("name").Value).FirstOrDefault();
As a prerequisite to the above, and so intellisense picks it up, make sure that you import the System.Linq and System.Xml.Linq namespaces.
This will get you all the descendants with the tag "game"
You just need the FirstOrDefault() to get the only record if it exists.
var q = from c in loaded.Descendants("game")
select new { Name = c.Element("name").Value
Description = c.Element("description").Value
};
q.FirstOrDefault();
You query is actually correct (tested and worked for me) for extracting the value of the name node - the (string) cast that you are doing will extract the value of the name node as string and not give you the node object itself, this is one of the shortcuts built into Linq to Xml. All that is left is to print out the name:
Response.Write(q);
var q = (from c in loaded.Descendants("game") select (string)c.Element("name")).SingleOrDefault();
Console.WriteLine(q);
is enough. or to avoid the cast
var q = (from c in loaded.Descendants("game") select c.Element("name").Value).SingleOrDefault();
Console.WriteLine(q);
I’m trying to use Linq XML to select a number of nodes and the children but getting terrible confused!
In the example XML below I need to pull out all the <MostWanted> and all the Wanted with their child nodes but without the other nodes in between the Mostwanted and Wanted nodes.
This because each MostWanted can be followed by any number of Wanted and the Wanted relate to the preceding Mostwanted.
I’m even confusing myself typing this up!!!
How can I do this in C#??
<root>
<top>
<NotWanted3>
</NotWanted3>
<MostWanted>
<UniqueKey>1</UniqueKey>
<QuoteNum>1</QuoteNum>
</MostWanted>
<NotWanted2>
<UniqueKey>1</UniqueKey>
<QuoteNum>1</QuoteNum>
</NotWanted2>
<NotWanted1>
<UniqueKey>0001</UniqueKey>
</NotWanted1>
<Wanted>
<Seg>
<SegNum>1</SegNum>
</Seg>
</Wanted>
<Wanted>
<Seg>
<SegNum>2</SegNum>
</Seg>
</Wanted>
<NotWanted>
<V>x</V>
</NotWanted>
<NotWanted3>
</NotWanted3>
<MostWanted>
<UniqueKey>1</UniqueKey>
<QuoteNum>1</QuoteNum>
</MostWanted>
<NotWanted2>
<UniqueKey>1</UniqueKey>
<QuoteNum>1</QuoteNum>
</NotWanted2>
<NotWanted1>
<UniqueKey>0002</UniqueKey>
</NotWanted1>
<Wanted>
<Seg>
<SegNum>3</SegNum>
</Seg>
</Wanted>
<Wanted>
<Seg>
<SegNum>4</SegNum>
</Seg>
</Wanted>
<NotWanted>
<V>x</V>
</NotWanted>
</top>
</root>
Why don't you just use:
XName wanted = "Wanted";
XName mostWanted = "MostWanted";
var nodes = doc.Descendants()
.Where(x => x.Name == wanted || x.Name == mostWanted);
That will retrieve every element called "Wanted" or "MostWanted". From each of those elements you can get to the child elements etc.
If this isn't what you're after, please clarify your question.
Is there a way to search an XDocument without knowing the namespace? I have a process that logs all SOAP requests and encrypts the sensitive data. I want to find any elements based on name. Something like, give me all elements where the name is CreditCard. I don't care what the namespace is.
My problem seems to be with LINQ and requiring a xml namespace.
I have other processes that retrieve values from XML, but I know the namespace for these other process.
XDocument xDocument = XDocument.Load(#"C:\temp\Packet.xml");
XNamespace xNamespace = "http://CompanyName.AppName.Service.Contracts";
var elements = xDocument.Root
.DescendantsAndSelf()
.Elements()
.Where(d => d.Name == xNamespace + "CreditCardNumber");
I really want to have the ability to search xml without knowing about namespaces, something like this:
XDocument xDocument = XDocument.Load(#"C:\temp\Packet.xml");
var elements = xDocument.Root
.DescendantsAndSelf()
.Elements()
.Where(d => d.Name == "CreditCardNumber")
This will not work because I don't know the namespace beforehand at compile time.
How can this be done?
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
<s:Body xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<Request xmlns="http://CompanyName.AppName.Service.ContractA">
<Person>
<CreditCardNumber>83838</CreditCardNumber>
<FirstName>Tom</FirstName>
<LastName>Jackson</LastName>
</Person>
<Person>
<CreditCardNumber>789875</CreditCardNumber>
<FirstName>Chris</FirstName>
<LastName>Smith</LastName>
</Person>
...
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
<s:Body xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<Request xmlns="http://CompanyName.AppName.Service.ContractsB">
<Transaction>
<CreditCardNumber>83838</CreditCardNumber>
<TransactionID>64588</FirstName>
</Transaction>
...
As Adam precises in the comment, XName are convertible to a string, but that string requires the namespace when there is one. That's why the comparison of .Name to a string fails, or why you can't pass "Person" as a parameter to the XLinq Method to filter on their name.
XName consists of a prefix (the Namespace) and a LocalName. The local name is what you want to query on if you are ignoring namespaces.
Thank you Adam :)
You can't put the Name of the node as a parameter of the .Descendants() method, but you can query that way :
var doc= XElement.Parse(
#"<s:Envelope xmlns:s=""http://schemas.xmlsoap.org/soap/envelope/"">
<s:Body xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance"" xmlns:xsd=""http://www.w3.org/2001/XMLSchema"">
<Request xmlns=""http://CompanyName.AppName.Service.ContractA"">
<Person>
<CreditCardNumber>83838</CreditCardNumber>
<FirstName>Tom</FirstName>
<LastName>Jackson</LastName>
</Person>
<Person>
<CreditCardNumber>789875</CreditCardNumber>
<FirstName>Chris</FirstName>
<LastName>Smith</LastName>
</Person>
</Request>
</s:Body>
</s:Envelope>");
EDIT : bad copy/past from my test :)
var persons = from p in doc.Descendants()
where p.Name.LocalName == "Person"
select p;
foreach (var p in persons)
{
Console.WriteLine(p);
}
That works for me...
You could take the namespace from the root-element:
XDocument xDocument = XDocument.Load(#"C:\temp\Packet.xml");
var ns = xDocument.Root.Name.Namespace;
Now you can get all desired elements easily using the plus-operator:
root.Elements(ns + "CreditCardNumber")
I think I found what I was looking for. You can see in the following code I do the evaluation Element.Name.LocalName == "CreditCardNumber". This seemed to work in my tests. I'm not sure if it's a best practice, but I'm going to use it.
XDocument xDocument = XDocument.Load(#"C:\temp\Packet.xml");
var elements = xDocument.Root.DescendantsAndSelf().Elements().Where(d => d.Name.LocalName == "CreditCardNumber");
Now I have elements where I can encrypt the values.
If anyone has a better solution, please provide it. Thanks.
There's a couple answers with extension methods that have been deleted. Not sure why. Here's my version that works for my needs.
public static class XElementExtensions
{
public static XElement ElementByLocalName(this XElement element, string localName)
{
return element.Descendants().FirstOrDefault(e => e.Name.LocalName == localName && !e.IsEmpty);
}
}
The IsEmpty is to filter out nodes with x:nil="true"
There may be additional subtleties - so use with caution.
If your XML documents always defines the namespace in the same node (Request node in the two examples given), you can determine it by making a query and seeing what namespace the result has:
XDocument xDoc = XDocument.Load("filename.xml");
//Initial query to get namespace:
var reqNodes = from el in xDoc.Root.Descendants()
where el.Name.LocalName == "Request"
select el;
foreach(var reqNode in reqNodes)
{
XNamespace xns = reqNode.Name.Namespace;
//Queries making use of namespace:
var person = from el in reqNode.Elements(xns + "Person")
select el;
}
I a suffering from a major case of "I know that is the solution, but I am disappointed that that is the solution"... I recently wrote a query like the one below (which I will shortly replace, but it has educational value):
var result = xdoc.Descendants("{urn:schemas-microsoft-com:rowset}data")
.FirstOrDefault()?
.Descendants("{#RowsetSchema}row");
If I remove the namespaces from the XML, I can write the same query like this:
var result = xdoc.Descendants("data")
.FirstOrDefault()?
.Descendants("row");
I plan to write my own extension methods that should allow me to leave the namespaces alone and search for nodes like this:
var result = xdoc.Descendants("rs:data")
.FirstOrDefault()?
.Descendants("z:row");
//'rs:' {refers to urn:schemas-microsoft-com:rowset}
//'z:' {refers to xmlns:z=#RowsetSchema}
My comments just below the code point to how I would like to hide the ugliness of the solution in an Extension Methods library. Again, I'm aware of the solutions posted earlier - but I wish the API itself handled this more fluently. (See what I did there?)
Just use the Descendents method:
XDocument doc = XDocument.Load(filename);
String[] creditCards = (from creditCardNode in doc.Root.Descendents("CreditCardNumber")
select creditCardNode.Value).ToArray<string>();
I'm working on a program that needs to be able to load object-properties from an XML file. These properties are configurable by the user and XML makes sense to me to use.
Take the following XML document.
<?xml version="1.0" encoding="utf-8" ?>
<udpcommands>
<command name="requser">
<cvar name="reqchallege" value="false" />
</command>
<command name="reqprocs">
<cvar name="reqchallenge" value="false" />
</command>
</udpcommands>
I need to be able to load values from the cvars above to properties. I'm think Linq-To-XML would be good for it (I'm looking for applications of Linq so I can learn it). I've got a Linq-to-XML query done to select the right "command" based on the name.I was reading MSDN for help on this.
The following code snippet goes in a constructor that takes the parameter "string name" which identifies the correct XML <command> to pull.
I would like to have one linq statement to pull each <cvar> out of that XML given the section name, dumping everything to an IEnumerable. Or, I'm looking for a better option perhaps. I'm open for anything really. I would just like to use Linq so I can learn it better.
XElement doc = XElement.Load("udpcommands.xml");
IEnumerable<XElement> a = from el in doc.Elements()
where el.FirstAttribute.Value == name
select el;
foreach (var c in a)
{
Console.WriteLine(c);
}
The above code snippet outputs the following to the console:
<command name="requser">
<cvar name="reqchallege" value="false" />
</command>
Something like this should do:
var result =
doc.Elements("command")
.Single( x => x.Attribute("name").Value == name)
.Elements("cvar");
This will give you an IEnumerable<XElement> where each XElement represents a cvar in the specified command.
Note that if the specified command does not exist, the call to Single will cause an error. Likewise if the specified attribute is not found on the command.
EDIT As per your comments, you could do something along the lines of:
// Result will be an XElement,
// or null if the command with the specified attribute is not found
var result =
doc.Elements("command")
// Note the extra condition below
.SingleOrDefault( x => x.Attribute("name")!=null && x.Attribute("name").Value == name)
if(result!=null)
{
// results.Elements() gives IEnumerable<XElement>
foreach(var cvar in results.Elements("cvar"))
{
var cvarName = cvar.Attribute("name").Value;
var cvarValue = Convert.ToBoolean( cvar.Attribute("value").Value );
}
}