Beispiel #1
0
        public static XElement ToXml(this IGraph g, Prologue prolog)
        {
            XNamespace rdf      = "http://www.w3.org/1999/02/22-rdf-syntax-ns#";
            var        prefixes = new Dictionary <string, string>();

            var RDF = new XElement(rdf + "RDF");

            foreach (var s in g.GetAllSubjects())
            {
                XAttribute id = null;
                if (s is IIriNode)
                {
                    id = new XAttribute(rdf + "about", ((ObjectVariants)s).Content);
                }
                else if (s is IBlankNode)
                {
                    id = new XAttribute(rdf + "nodeID", s.ToString());
                }
                else
                {
                    throw new ArgumentOutOfRangeException();
                }
                RDF.Add(new XElement(rdf + "Description", id,
                                     g.GetTriplesWithSubject(s)
                                     .Select(t =>
                {
                    string p;
                    NamespaceLocalName ns = Prologue.SplitUri(t.Predicate.Content.ToString());
                    //if (!prefixes.TryGetValue(ns.@namespace, out p))
                    //{
                    //    RDF.Add);
                    //}
                    var x = new XElement(XName.Get("ns", ns.localname), new XAttribute(XNamespace.Xmlns + "ns", ns.@namespace));
                    if (t.Object is IIriNode)
                    {
                        x.Add(new XAttribute(rdf + "resource", t.Predicate.Content));
                    }
                    else if (t.Object is ILiteralNode)
                    {
                        ILiteralNode ol = ((ILiteralNode)t.Object);
                        if (ol is ILanguageLiteral)
                        {
                            x.Add(new XAttribute(XNamespace.Xml + "lang",
                                                 ((ILanguageLiteral)t.Object).Lang));
                        }
                        else if (!(ol is IStringLiteralNode))
                        {
                            x.Add(new XAttribute(rdf + "datatype", ol.DataType));
                        }

                        x.Add(ol.Content);
                    }
                    else if (t.Object is IBlankNode)
                    {
                        x.Add(new XAttribute(rdf + "nodeID", t.Object.ToString()));
                    }
                    else
                    {
                        throw new ArgumentOutOfRangeException();
                    }
                    return(x);
                })));
            }
            ;

            return(RDF);
        }
        public static XElement ToXml(this IGraph g, Prologue prolog)
        {
            XNamespace rdf = "http://www.w3.org/1999/02/22-rdf-syntax-ns#";
            var prefixes = new Dictionary<string, string>();

            var RDF = new XElement(rdf + "RDF");
            foreach (var s in g.GetAllSubjects())
            {
                XAttribute id = null;
                if (s is IIriNode)
                {
                    id = new XAttribute(rdf + "about", ((ObjectVariants) s).Content);
                }
                else if (s is IBlankNode)
                {
                    id = new XAttribute(rdf + "nodeID", s.ToString());
                }
                else
                {
                    throw new ArgumentOutOfRangeException();
                }
                RDF.Add(new XElement(rdf + "Description", id,
                    g.GetTriplesWithSubject(s)
                    .Select(t=>
                    {
                        string p;
                        NamespaceLocalName ns = Prologue.SplitUri(t.Predicate.Content.ToString());
                        //if (!prefixes.TryGetValue(ns.@namespace, out p))
                        //{
                        //    RDF.Add);
                        //}
                        var x = new XElement(XName.Get("ns", ns.localname), new XAttribute(XNamespace.Xmlns + "ns", ns.@namespace));
                        if (t.Object is IIriNode)
                        {
                            x.Add(new XAttribute(rdf + "resource", t.Predicate.Content));
                        }
                        else if (t.Object is ILiteralNode)
                        {
                            ILiteralNode ol = ((ILiteralNode) t.Object);
                            if (ol is ILanguageLiteral)
                                x.Add(new XAttribute(XNamespace.Xml + "lang",
                                    ((ILanguageLiteral) t.Object).Lang));
                            else if (!(ol is IStringLiteralNode))
                                x.Add(new XAttribute(rdf + "datatype", ol.DataType));

                            x.Add(ol.Content);
                        }
                        else if (t.Object is IBlankNode)
                        {
                            x.Add(new XAttribute(rdf + "nodeID", t.Object.ToString()));
                        }
                        else
                        {
                            throw new ArgumentOutOfRangeException();
                        }
                        return x;
                    })));
            };

            return RDF;
        }