Convert XmlElement to XElement with Elements("ElementName") Returns No Results

I'm using the following code to convert a XmlElement to XElement

public staic XElement ToXElement(this XmlNode node) {
    XElement element = null;
    if (null != node) {
        element = XElement.Parse(node.OuterXml);
    }
    return element;
}

However when I call Elements() or Elements("ElementName") I get no results. I do however get results from calling Nodes().

Why don't the elements come up from calling Elements and what's the difference between the two methods?

Here is a snippit of the xml I'm parsing.

<Feature xmlns="http://schemas.microsoft.com/sharepoint/">
    <ElementManifests>
        <ElementFile Location="Path/file.xml"/>
    </ElementManifests>
</Feature>

Answers


You are probably not using the namespace correctly. Both of these methods work correctly for me:

XElement root = XElement.Load("test.xml"); //or result of ToXElement
foreach(var item in root.Elements())
{
    Console.WriteLine(item.Name);
}

XNamespace ns = "http://schemas.microsoft.com/sharepoint/";
var manifestsNode = root.Element(ns + "ElementManifests");

Given that you don't know the difference between the Elements() (get all direct children) and Element() (get one specific direct child element) you should start with a Linq to Xml tutorial.


Need Your Help

Azure webrole act as proxy for third party api

asp.net api azure

I'm currently looking into developing an azure hosted web service to act as a proxy between my mobile app and a third party web service in order to keep my API secret. I haven't a great deal if

Eclipse & JSP: java.lang.NoSuchMethodError: javax.servlet.jsp.tagext.TagAttributeInfo.<init>(Ljava/lang/String;ZLjava/lang/String;ZZ)V

eclipse jsp eclipse-3.4

I'm using Eclipse 3.4 with WTP 3.0.2 and running a fairly large Dynamic Web Project. I've set up the project so that I can access it at http://127.0.0.1:8080/share/ but whenever I do, I get the fol...