Question

How can i find complexType element knowing it's name(station)? I'm beginner Here is an entire xsd code: http://pastebin.com/ymuPDCCb This doesn't work.

private XElement GetComplexType(string typeName)
{
    XElement complexType = xsdSchema.Elements("complexType")
        .Where(a => a.Attributes("name").FirstOrDefault() != null && a.Attribute("name").Value==typeName)
        .FirstOrDefault();

    return complexType;
}
Was it helpful?

Solution

You have to include the xs namespace and you need to query all levels using Descendants() instead of Elements(), like so:

private static void ParseXml()
{
    XDocument doc = XDocument.Load(@"C:\schema.xml");

    if (doc != null)
    {
        XElement nodes = GetComplexType("station", doc);

        if (nodes != null)
        {
            Console.WriteLine("station found...");
        }
        else
        {
            Console.WriteLine("station NOT found!!");
        }
    }
}

private static XElement GetComplexType(string typeName, XDocument xsdSchema)
{
    XNamespace ns = "http://www.w3.org/2001/XMLSchema";
    XElement complexType = xsdSchema.Descendants(ns + "complexType")
        .Where(a => a.Attributes("name").FirstOrDefault() != null && a.Attribute("name").Value == typeName)
        .FirstOrDefault();

    return complexType;
}

Check this SO answer for more info on Elements vs Descendants

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top