Get node from UrlName in Umbraco - c#

I am working on an Umbraco set up (Umbraco 7) and I need to be able to load a node from a node.UrlName.
I tried Xpath but that didn't work. Here is my xpath:
var facNode = umbraco.NodeFactory.Node.GetNodeByXpath("/*[#UrlName='" + urlName + "']");
I have also tried a few variations of this without /, with // and just looking for an id instead of a UrlName.
This seems to be something fairly simple but I seem to be missing something. I am pretty new to Umbraco so any help would be greatly appreciated.
Id there a better way than xpath? What class should I be using for this?
Thanks in advance!

Well, there are a couple of things here. Firstly, the xpath is case-sensitive, so you need to target #urlName not #UrlName.
Also it's worth pointing out that trying to locate a node by the urlName property isn't a very good strategy because multiple nodes could have an identical urlName property. This is possible because Umbraco only insists that the urlName property is unique where the node shares the same parent.
This makes sense because (e.g. when grouping news/blog articles) you may want a node called October under a node called 2014 but also a node called October under a parent called 2013. In this scenario, your xpath would locate both October nodes and therefore not work.
As far as a btter way, it depends on what the context is and what you are trying to achieve. You certainly should be avoiding using the umbraco.NodeFactory.Node as from Umbraco 4.8 the standard has been to use the IPublishedContent interface which is arguably more flexible.

Related

Sort XmlNodeList with a specific Node value

I searched the web for this, but I am not able to find any related solution.
I have following XML
<Table>
<Entity>Employee</Entity>
<EntityID>2786</EntityID>
<Goal>
<GoalId>31931</GoalId>
<FilterData>LastModifiedOn¥2014-03-20T18:11:01.0000000+05:30ÆActiveTaskCount¥0</FilterData>
</Goal>
<Goal>
<GoalId>31932</GoalId>
<FilterData>LastModifiedOn¥2014-03-22T15:26:09.0000000+05:30ÆActiveTaskCount¥0</FilterData>
</Goal>
<Goal>
<GoalId>31932</GoalId>
<FilterData>LastModifiedOn¥2014-03-22T09:25:00.0000000+05:30ÆActiveTaskCount¥0</FilterData>
</Goal>
</Table>
From above XML when I read the data I got 2 separate DataTables; 1 for Employees and another one for related Goals.
What I needed is I want to sort all the Goals related to an employee with respect to LastModifiedOn from FilterData node.
NOTE: I am getting the LastModifiedOn value by split node value like this
nodevalue.Split('Æ')[0].Split('¥')[1]
Right now I am using System.XML namespace for doing operations. I also looked at LINQ TO XML but I am unable to make it work.
I am getting the XMLNodelist by following code
XmlNodeList GoalNodesList = doc.DocumentElement.SelectNodes("/NewDataSet/Table[Entity='Employee' and EntityID='" + EntityId + "']/Goal");
Here I want the Sorted Goals (with respect to LastModifiedOn).
I also looked at some useful links but didn't get any idea so far
Sorting XML nodes based on DateTime attribute C#, XPath
XMLdocument Sort
I am ready to convert the code into LINQ TO XML but needed a brief example.
One thing you could try is creating a collection of key value pairs where key is the goal and value is the date. You could then override the CompareTo method by comparing the dates and then you could just use [collection name].Sort(). Hope this helps

How to filter xml nodes in sql server?

How to add second condition to the following query?
SELECT c.value('#tray','VARCHAR(MAX)')
FROM #L_XML.nodes('/XDOC/DOC[#TYPE="Q"]') T(c)
so that I could filter on attribute: type = Q or S?
Where Can I find a detailed manual about xml nodes?
SELECT c.value('#tray','VARCHAR(MAX)')
FROM #L_XML.nodes('/XDOC/DOC[#TYPE = ("Q", "S")]') T(c)
A very good starting point is SQLXML where an exhaustive list of basic reads and writes are covered on a How To basis. Quite handy and certainly worth a bookmark in my book!
In regard to your first question: have a look here, that might give you what you're looking for. I think your problem might be in the quotes.

How to get all the element from HTML who having same class attribute?

I developing one asp.net application in that I using htmlagility dll to get all <div> tags which is same class attributes..
How to get all elements who have same class from whole html page,
I getting top 1 div who having class='myclass' but in,
my case I want to all div tags who have 'myclass' class.
var vardoc = web.Load("<any website url>");
var varmyclass = doc.DocumentNode.SelectSingleNode("//div[#class='myclass']");
when I used above method then I getting inner html content of the 'myclass' but in my html contents there are many div tags who have class 'myclass'.
I want to get all <div> who have same class using HTMLagility or other
Don't select a single node in that case.
You are using SelectSingleNode, which will return only one node.
Use SelectNodes instead:
var varmyclass = doc.DocumentNode.SelectNodes("//div[#class='myclass']");
You're pretty much searching an XML document for these nodes.
First off, you're selecting a single node which will only return 1. You need to use SelectNodes.
example:
var varmyclass = doc.DocumentNode.SelectNodes("//div[#class='myclass']");
Update - Removed my Xpath suggestion as it didn't work as I thought it would...
Two issues, first one is that you should be using SelectNodes() instead as the other answers have pointed out since you want to select multiple nodes.
Also, your XPath is a bit restrictive. It will only get div elements which has only one class, myclass but doesn't include those that have multiple classes. I suspect you want to include those as well. Rather than checking if the class is equal, check if it contains it.
var xpath = "//div[contains(#class,'myclass')]";
var query = doc.DocumentNode.SelectNodes(xpath);

how to load a hashtable from a simple xml file using xmltextreader

using xmltextreader, how would I load a hashtable.
XML:
<base><user name="john">2342343</user><user name="mark">239099393</user></base>
This was asked before but it was using some funky linq that I am not fully comfortable with just yet.
Well, the LINQ to XML solution is really easy, so I suggest we try to make you comfortable with that instead of creating a more complex solution. Here's the code, with plenty of explanation...
// Load the whole document into memory, as an element
XElement root = XElement.Load(xmlReader);
// Get a sequence of users
IEnumerable<XElement> users = root.Elements("user");
// Convert this sequence to a dictionary...
Dictionary<string, string> userMap = users.ToDictionary(
element => element.Attribute("name").Value, // Key selector
element => element.Value); // Value selector
Of course you could do this all in one go - and I'd probably combine the second and third statements. But that's about as conceptually simple as it's likely to get. It would become more complicated if you wanted to put error handling around the possibility that a user element might not have a name, admittedly. (This code will throw a NullReferenceException in that case.)
Note that this assumes you want the name as the key and id as value. If you want the hashtable the other way round, just switch the order of the lambda expressions.

Setting attributes in an XML document

I'm writing one of my first C# programs. Here's what I'm trying to do:
Open an XML document
Navigate to a part of the XML tree and select all child elements of type <myType>
For each <myType> element, change an attribute (so <myType id="oldValue"> would become <myType id="newValue">
Write this modified XML document to a file.
I found the XmlDocument.SelectNodes method, which takes an XPath expression as its argument. However, it returns an XmlNodeList. I read a little bit about the difference between an XML node and an XML element, and this seems to explain why there is no XmlNode.SetAttribute method. But is there a way I can use my XPath expression to retrieve a list of XmlElement objects, so that I can loop through this list and set the id attributes for each?
(If there's some other easier way, please do let me know.)
Simply - it doesn't know if you are reading an element or attribute. Quite possibly, all you need is a cast here:
foreach(XmlElement el in doc.SelectNodes(...)) {
el.SetAttribute(...);
}
The SelectNodes returns an XmlNodeList, but the above treats each as an XmlElement.
I am a big fan of System.Xml.Linq.XDocument and the features it provides.
XDocument xDoc = XDocument.Load("FILENAME.xml");
// assuming you types is the parent and mytype is a bunch of nodes underneath
IEnumerable<XElement> elements = xdoc.Element("types").Elements("myType");
foreach (XElement type in elements)
{
// option 1
type.Attribute("id").Value = NEWVALUE;
// option 2
type.SetAttributeValue("id", NEWVALUE);
}
Option 1 or 2 works but I prefer 2 because if the attribute doesn't exist this'll create it.
I'm sitting at my Mac so no .NET for me...
However, I think that you can cast an XmlNode to an XmlElement via an explicit cast.
You should be able to cast the XmlElement to an XmlNode then and get it's children Nodes using something like XmlNode.ChildNodes.

Categories

Resources