Try to explain better :-p
I have 2 strings (xml) and a function

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
let grafoWithoutXmlns = "<?xml version=\"1.0\" encoding=\"UTF-8\"?> 
<graphml>
<graph id=\"Grafo\" edgedefault=\"directed\" 
             parse.nodes=\"6\" parse.edges=\"6\" 
             parse.nodeids=\"canonical\" parse.edgeids=\"canonical\" 
             parse.order=\"nodesfirst\">
    <node id=\"n3\">
    <data key=\"nV\">3</data>
    <data key=\"nR\">15</data>
    </node>
    <node id=\"n2\">
    <data key=\"nV\">0</data>
    <data key=\"nR\">9</data>
    </node>
    </graph>
</graphml>"

let grafoXmlns =  "<?xml version=\"1.0\" encoding=\"UTF-8\"?> 
<graphml <b>xmlns=\"http://graphml.graphdrawing.org/xmlns\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:schemaLocation=\"http://graphml.graphdrawing.org/xmlns  http://graphml.graphdrawing.org/xmlns/1.0/graphml.xsd\</b>">
<graph id=\"Grafo\" edgedefault=\"directed\" 
             parse.nodes=\"6\" parse.edges=\"6\" 
             parse.nodeids=\"canonical\" parse.edgeids=\"canonical\" 
             parse.order=\"nodesfirst\">
    <node id=\"n3\">
    <data key=\"nV\">3</data>
    <data key=\"nR\">15</data>
    </node>
    <node id=\"n2\">
    <data key=\"nV\">0</data>
    <data key=\"nR\">9</data>
    </node>
    </graph>
</graphml>"

let getGrafo xml =
    let doc = new XmlDocument() in
        doc.LoadXml xml;
        doc.SelectNodes ("/graphml/graph/node/data/text()")
            |> Seq.cast<XmlNode>
            |> Seq.map (fun node -> node.Value)
            |> String.concat " "

The results are...

1
2
3
4
5
getGrafo grafoWithoutXmlns
val it : string = "3 15 0 9"

getGrafo grafoXmlns
val it : string = ""

I tried different things, but nothing works if you have declared an xmlns, always empty (or error :-P)

By on 2/4/2011 1:59 AM ()

hi,

try the more functional (and easier) XLinq (System.Xml.Linq):

1
2
3
4
5
6
let getGrafo xml =
    let doc = XDocument.Parse xml |> (fun x -> x.Descendants())

    doc |> Seq.filter (fun x -> x.Name.LocalName = "data")
        |> Seq.map (fun x -> x.Value)
        |> String.concat " "
By on 2/5/2011 11:02 PM ()

Thank you very much, it works!

Now, I have to learn to use XLinq ;-)

By on 2/6/2011 5:08 AM ()
IntelliFactory Offices Copyright (c) 2011-2012 IntelliFactory. All rights reserved.
Home | Products | Consulting | Trainings | Blogs | Jobs | Contact Us | Terms of Use | Privacy Policy | Cookie Policy
Built with WebSharper