0

My goal is to save the data contained in the ValueReference node, TimeInstant attribute, and timePosition node into variables. I am able to get the value of the valueReference node (the un-commented section works), but not the other two. Any help would be greatly appreciated.

Here is the code that I am working on:

public void LinqToXml()
{
    XNamespace sosNamespace = XNamespace.Get("http://www.opengis.net/sos/2.0");
    XNamespace fesNamespace = XNamespace.Get("http://www.opengis.net/fes/2.0");
    XNamespace gmlNamespace = XNamespace.Get("http://www.opengis.net/gml/2.0");
    var root = XElement.Load(@"C:\Working Directory\OGCSOS.Service\OGCSOS.Service\Resources\GetObservation_TemporalFilter.xml");
    var a = (from level in root.Descendants(sosNamespace + "temporalFilter")
             select new
             {
                 valueReference = (string)level.Descendants(fesNamespace + "After")
                                               .Elements(fesNamespace + "ValueReference")
                                               .First(),
                 /*timeInstant = (string)level.Descendants(fesNamespace + "After")
                                               .Elements(gmlNamespace + "TimeInstant")
                                               .Attributes(gmlNamespace + "id")
                                               .First()*/
                 /*timePosition = (string)level.Descendants(fesNamespace + "After")
                                             .Elements(gmlNamespace + "TimeInstant")
                                             .First()*/
             }).ToList();

And here is the XML I am trying to read:

    <?xml version="1.0" encoding="UTF-8"?>
<sos:GetObservation xmlns="http://www.opengis.net/sos/2.0" service="SOS" version="2.0.0" 
                    xmlns:sos="http://www.opengis.net/sos/2.0" xmlns:fes="http://www.opengis.net/fes/2.0" 
                    xmlns:gml="http://www.opengis.net/gml/3.2" xmlns:swe="http://www.opengis.net/swe/2.0" 
                    xmlns:swes="http://www.opengis.net/swes/2.0" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.opengis.net/sos/2.0
http://schemas.opengis.net/sos/2.0/sos.xsd">

    <!--identifier of an offering-->
    <offering>HG.Logger@DJK001</offering>

    <!--identifier of an observed property-->
    <observedProperty>HG</observedProperty>

    <!--optional temporal filter restricting the results which shall be returned-->
    <temporalFilter>
        <fes:After>
            <fes:ValueReference>phenomenonTime</fes:ValueReference>
            <gml:TimeInstant gml:id="startPosition">
                <gml:timePosition>2008-03-01T17:44:15.000+00:00</gml:timePosition>
            </gml:TimeInstant>
        </fes:After>
    </temporalFilter>

    <featureOfInterest>DJK001</featureOfInterest>

</sos:GetObservation>
5
  • Also, if somebody can suggest an alternative approach that might be easier I am all ears. Commented Oct 25, 2012 at 16:40
  • can there be multiple temporalFilter Commented Oct 25, 2012 at 16:53
  • 1
    Well XNamespace sosNamespace = XNamespace.Get("http://www.opengis.net/sos/2.0"); can be shortened to XNamespace sosNamespace = "http://www.opengis.net/sos/2.0";. And as the idea behind the XNamespace class is to use instances to construct XNames by concatenation I would use a short variable name instead e.g. XNamespace sos = "http://www.opengis.net/sos/2.0";, then when calling axis methods you have shorter expressions e.g. foo.Elements(sos + "Bar"). Commented Oct 25, 2012 at 16:54
  • For now, there will only be one temporal filter node. Commented Oct 25, 2012 at 17:05
  • @MartinHonnen Thanks for the suggestions, I'll make the changes. Commented Oct 25, 2012 at 17:06

4 Answers 4

2

Your gml namespace is not correct, after changing it to

 XNamespace gmlNamespace = XNamespace.Get("http://www.opengis.net/gml/3.2");

you can use

timeInstant = level.Descendants(fesNamespace + "After")
                   .First()
                   .Element(gmlNamespace + "TimeInstant")
                   .Attribute(gmlNamespace + "id")
                   .Value,

timePosition = level.Descendants(fesNamespace + "After")
                    .First()
                    .Element(gmlNamespace + "TimeInstant")
                    .Value
Sign up to request clarification or add additional context in comments.

Comments

1

You should do it like this

XNamespace sosNamespace = "http://www.opengis.net/sos/2.0";
XNamespace fesNamespace = "http://www.opengis.net/fes/2.0";
XNamespace gmlNamespace = "http://www.opengis.net/gml/3.2";
//you had used 2.0 instead of 3.2
var root = XElement.Load(@"C:\WorkingDirectory\OGCSOS.Service\OGCSOS.Service\Resources\GetObservation_TemporalFilter.xml");
var yourList=root.Descendants(sosNamespace+"temporalFilter").Descendants(fesNamespace+"After").Select(x=>
new
{
ValueReference=x.Element(fesNamespace+"ValueReference").Value,
timeInstant=x.Element(gmlNamespace+"TimeInstant").Attribute(gmlNamespace+"id").Value,
timePosition=x.Element(gmlNamespace+"TimeInstant").Element(gmlNamespace+"timePosition").Value
}
);

yourList contains all the data

Comments

1

you can also use good old XPath

    var doc = new XPathDocument("1.xml");
    var nav = doc.CreateNavigator();
    var mng = new XmlNamespaceManager(nav.NameTable);
    mng.AddNamespace("sos", "http://www.opengis.net/sos/2.0");
    mng.AddNamespace("fes", "http://www.opengis.net/fes/2.0");
    mng.AddNamespace("gml", "http://www.opengis.net/gml/3.2");
    var valueReference = nav.SelectSingleNode("//sos:GetObservation/sos:temporalFilter/fes:After/fes:ValueReference[1]", mng).TypedValue;
    var TimeInstant = nav.SelectSingleNode("//sos:GetObservation/sos:temporalFilter/fes:After/gml:TimeInstant/@gml:id", mng).TypedValue;
    var timePosition = nav.SelectSingleNode("//sos:GetObservation/sos:temporalFilter/fes:After/gml:TimeInstant/gml:timePosition[1]", mng).TypedValue;

1 Comment

I did also give this a shot, but figured I was too deep into learning LINQ to continue. Thanks for the good example.
0

TRY THIS CODE:

XmlDocument xmlSnippet = new XmlDocument();
 xmlSnippet.Load(@"C:\Working Directory\OGCSOS.Service\OGCSOS.Service\Resources\GetObservation_TemporalFilter.xml");

 //Selects all the similar nodes by tag Name.......
               XmlNodeList xmlSnippetNodes = xmlSnippet.GetElementsByTagName("fes:After");

 //Checking if any any xmlSnippetNode has matched.
               if (xmlSnippetNodes != null)
               {
                   //Checks if the matched xmlSnippetNode that has fes:After attribute is not  NULL


//Stores the value of the matched tag.
                          var valueReference= xmlSnippetNodes.Item(0).Value;

                   var timeInstance=xmlSnippetNodes.Item(1).Attributes["gml:id"].Value;
                 var timePosition =xmlSnippetNodes.Item(1).InnerXml.Name;


                           //Return True if updated correctly.
                           isUpdated = true;


                   }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.