I am trying to create a protégé readable ontology from .NET.
I started with a 4GB .nt file and parsed out the needed classes and instances that I wish to use. Those are stored in memory since I got it down to less than 1 minute and roughly 1GB. They are in the form of Dictionary<String,HashSet<String>> right now. The next step is to take that data and move it into an OWL Ontology. Is there anywhere to start with how to manually loop through and do this? All of my research points me to using Manchester OWL, but everything I can find is for use with existing tools that don't fit my needs. I am looking for just doing a simple loop probably with LINQ to XML and I am unsure of how to format that or where to look to find how to do this.
Thanks
Jimmy
You can start with this
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
string definition =
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>" +
"<Ontology xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"" +
" xsi:schemaLocation=\"http://www.w3.org/2002/07/owl# http://www.w3.org/2009/09/owl2-xml.xsd\"" +
" xmlns=\"http://www.w3.org/2002/07/owl#\"" +
" xml:base=\"http://example.com/myOntology\"" +
" ontologyIRI=\"http://example.com/myOntology\">" +
"</Ontology>";
XDocument doc = XDocument.Parse(definition);
XElement ontology = (XElement)doc.FirstNode;
XNamespace ns = ontology.Name.Namespace;
ontology.Add(new XElement[] {
new XElement(ns + "Prefix", new XAttribute[] {
new XAttribute("name", "myOnt"),
new XAttribute("IRI", "http://example.com/myOntology#")
}),
new XElement(ns + "Import", "http://example.com/someOtherOntology")
});
}
}
}
Related
I am trying to update the content of multiple nodes that match a specific xpath. For example, in the below example, I want to update any attribute1 node that currently is '2' with 'Hello'. What is the best way to do this?
(DotNetFiddle Here)
using System;
using System.IO;
using System.Xml;
using System.Linq;
public class Program
{
public static void Main()
{
XmlDocument job = new XmlDocument();
job.LoadXml(#"<parent>" +
"<report>" +
"<main>" +
"<attribute1>2</attribute1>"+
"<attribute1>2</attribute1>"+
"<attribute1>3</attribute1>"+
"</main>"+
"</report>" +
"</parent>");
string newAttribute1Value = "Hello";
//How do I update both attribute1's where value=2?
}
This may could help you :
using System;
using System.IO;
using System.Xml;
using System.Linq;
public class Program
{
public static void Main()
{
XmlDocument job = new XmlDocument();
job.LoadXml(#"<parent>" +
"<report>" +
"<main>" +
"<attribute1>2</attribute1>"+
"<attribute1>2</attribute1>"+
"<attribute1>3</attribute1>"+
"</main>"+
"</report>" +
"</parent>");
string newAttribute1Value = "Hello";
//How do I update both attribute1's where value=2?
// getting the list of nodes with XPath query :
XmlNodeList nodes = job.SelectNodes("//attribute1[text()=2]");
foreach (XmlNode child in nodes)
{
// replacing the old value with the new value
child.InnerText = newAttribute1Value ;
}
}
I have an XML file content as below
and I need to modify line 4:
701.50,24.0 to 701.50,30.0
How can I do this?
<CableLossConfig>
<Std Val="CATM1">
<Path Val="TX1">
<Loss>701.50,24.0</Loss>
<Loss>710.50,24.0</Loss>
<Loss>713.50,24.0</Loss>
<Loss>779.50,23.0</Loss>
<Loss>782.00,23.0</Loss>
<Loss>784.50,23.0</Loss>
<Loss>826.50,30.0</Loss>
<Loss>836.50,30.0</Loss>
<Loss>846.50,30.0</Loss>
<Loss>1712.50,37.0</Loss>
<Loss>1732.50,37.0</Loss>
<Loss>1752.50,37.0</Loss>
<Loss>1852.50,37.0</Loss>
<Loss>1880.00,37.0</Loss>
<Loss>1907.50,37.0</Loss>
</Path>
</Std>
</CableLossConfig>
Using xml linq :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;
namespace ConsoleApplication1
{
class Program
{
const string FILENAME = #"c:\temp\test.xml";
static void Main(string[] args)
{
XDocument doc = XDocument.Load(FILENAME);
XElement tx1 = doc.Descendants("Path").Where(x => (string)x.Attribute("Val") == "TX1").FirstOrDefault();
List<XElement> losses = tx1.Elements("Loss").ToList();
XElement loss1 = losses[1];
loss1.SetValue("701.50,30.0");
}
}
}
If you would like a simple solution that does not require creating a C# model representing the structure of the xml file, I would suggest using the XDocument .net class (https://learn.microsoft.com/en-us/dotnet/api/system.xml.linq.xdocument?view=netframework-4.8)
I found a quite nice example here on stackoverflow as well where a similar question was asked: Edit specific Element in XDocument
I hope this helps!
- <SynchroAnimation Duration="10" EndTime="10000" ModelFileName="Lukes box (named).fbx" StartTime="0" SynchroVersion="5.2.2.0" Version="2">
- <Resource Identifier="AA67882D-9AC1-4450-90D1-2C1C7FF8F6BC" Name="Box Side - Growth" NumAnimatedInstances="1">
- <Resource3DRepresentation>
- <Entity3D Identifier="7D972BE6-9B4A-4A86-93C5-4256FB1DCE8F" Name="Basic Wall Wall-Ext_102Bwk-75Ins-100LBlk-12P [294917]">
+ <PreTransform>
<M0>0.003280840348452</M0>
</PreTransform>
+ <PostTransform>
<M0>304.799987792968750</M0>
</PostTransform>
</Entity3D>
</Resource3DRepresentation>
- <ResourceInstance>
- <VisibilityTimeline>
- <VisibilityKeyframe Interpolation="Discrete" Time="0" UseOriginal="false">
<VisibilityValue>0</VisibilityValue>
</VisibilityKeyframe>
- <VisibilityKeyframe Interpolation="Discrete" Time="1822" UseOriginal="false">
<VisibilityValue>100</VisibilityValue>
</VisibilityKeyframe>
</VisibilityTimeline>
- <ColorTimeline>
+ <ColorKeyframe Time="1822" UseOriginal="false">
+ <ColorValue>
<R>0.00</R>
<G>0.00</G>
<B>1.00</B>
</ColorValue>
</ColorKeyframe>
<ColorKeyframe Time="8066" UseOriginal="true" />
</ColorTimeline>
+ <TransformTimeline>
<TransformKeyframe Interpolation="Discrete" Time="1822" UseOriginal="true" />
</TransformTimeline>
</ResourceInstance>
</Resource>
+ <Resource Identifier="11F593C9-5A33-4E24-A41F-7F003404E346" Name="Box Side - Fade In" NumAnimatedInstances="1">
- <Resource3DRepresentation>
+ <Entity3D Identifier="88D87801-2C5D-4F20-BBB9-21566D230A33" Name="Basic Wall
MORE Resco
</SynchroAnimation>
snExported FBX XML
I'm trying to get the highlighted values from the attached image XML file. I need to get the values in "3D id" so it can be attached to the correct game object inside unity game engine with the correct metadata (i.e.: start time and date).
This is the code I currently have, but this is my first time dealing with XML files so any help would be appreciated.
using UnityEngine;
using System;
using System.Linq;
using System.Xml;
using System.Xml.Linq;
using System.Collections.Generic;
using System.Text;
using System.IO;
public class XML : MonoBehaviour
{
public string filepath = "filepath";
public string filename = "filename";
public string filetype = ".xml";
void start()
{
XDocument xDoc = XDocument.Load(filepath + filename + filetype);
var resourceinfo = from rescourse in xDoc.Descendants("Resource")
where rescourse.Attribute("Name").Value == "Growth"
select new
{
Resource = rescourse.Descendants("Resource").Descendants(),
ResourceInstance = rescourse.Descendants("Resource3DRepresentation").Descendants(),
};
foreach (var rescourse in resourceinfo)
{
string name = rescourse.Resource.ElementAt(1).Attribute("Name").Value;
Debug.Log(name);
}
}
I am using following code to read the specified XML
<?xml version=\"1.0\" encoding=\"UTF-8\"?>
<feed version=\"0.3\" xmlns=\"http://purl.org/atom/ns#\">
<title>Gmail - Inbox for xxxx#gmail.com</title>
<tagline>New messages in your Gmail Inbox</tagline>
<fullcount>1</fullcount>
<link rel=\"alternate\" href=\"https://mail.google.com/mail\" type=\"text/html\" />
<modified>2016-02-07T12:11:21Z</modified>
<entry>
<title>Access for less secure apps has been turned on</title>
<summary>Access for less secure apps has been turned on Hi Buddy, You recently changed your security settings so</summary>
<link rel=\"alternate\" href=\"https://mail.google.com/mail?account_id=agl.testauto#gmail.com&message_id=152bb8ccd28d824b&view=conv&extsrc=atom\" type=\"text/html\" />
<modified>2016-02-07T11:45:12Z</modified>
<issued>2016-02-07T11:45:12Z</issued>
<id>tag:gmail.google.com,2004:1525516088640373323</id>
<author>
<name>Google</name>
<email>no-reply#accounts.google.com</email>
</author>
</entry>
</feed>
Below code is being used and problem is I am not getting values for title element with last line of code.
response = Encoding.UTF8.GetString(objclient.DownloadData("https://mail.google.com/mail/feed/atom"));
response = response.Replace("<feed version*\"0.3\" xmlns=\"http://purl.org/atom/01#\">", "<feed>");
xdoc.LoadXml(response);
var nsmgr = new XmlNamespaceManager(xdoc.NameTable);
nsmgr.AddNamespace("feed", "http://purl.org/atom/ns#");
node = xdoc.SelectSingleNode("//feed:fullcount", nsmgr);
Variables.emailcount = Convert.ToInt16(node.InnerText);
System.Diagnostics.Debug.Write(Variables.emailcount);
tagline = xdoc.SelectSingleNode("//feed:tagline", nsmgr).InnerText;
node2 = xdoc.SelectSingleNode("//feed:entry", nsmgr);
message_subject = node2.SelectSingleNode("//feed/entry/title", nsmg).InnerText; ---- >>> Issue Line
Just wondering where could be the issue.
Thanks
try
message_subject = node2.SelectSingleNode("//feed:entry/feed:title",
nsmgr).InnerText;
note: you have used "feed" as the name of the namespace, so the title should also be qualified
Make your life easier by using SyndicationFeed defined into System.ServiceModel.Syndication namespace from System.ServiceModel assembly.
SyndicationFeed syndicationFeed = null;
using (var reader = XmlReader.Create("https://mail.google.com/mail/feed/atom"))
{
syndicationFeed = SyndicationFeed.Load(reader);
}
if(syndicationFeed != null)
{
foreach (SyndicationItem item in syndicationFeed.Items)
{
// Do everything you want here by checking properties you need from SyndicationItem item variable.
}
}
To know what are properties that SyndicationItem offer check this link.
I prefer xml linq
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;
namespace ConsoleApplication1
{
class Program
{
const int NUMBER_OF_XML = 3;
static void Main(string[] args)
{
string xml =
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>" +
"<feed version=\"0.3\" xmlns=\"http://purl.org/atom/ns#\">" +
"<title>Gmail - Inbox for xxxx#gmail.com</title>" +
"<tagline>New messages in your Gmail Inbox</tagline>" +
"<fullcount>1</fullcount>" +
"<link rel=\"alternate\" href=\"https://mail.google.com/mail\" type=\"text/html\" />" +
"<modified>2016-02-07T12:11:21Z</modified>" +
"<entry>" +
"<title>Access for less secure apps has been turned on</title>" +
"<summary>Access for less secure apps has been turned on Hi Buddy, You recently changed your security settings so</summary>" +
"<link rel=\"alternate\" href=\"https://mail.google.com/mail?account_id=agl.testauto#gmail.com&message_id=152bb8ccd28d824b&view=conv&extsrc=atom\" type=\"text/html\" />" +
"<modified>2016-02-07T11:45:12Z</modified>" +
"<issued>2016-02-07T11:45:12Z</issued>" +
"<id>tag:gmail.google.com,2004:1525516088640373323</id>" +
"<author>" +
"<name>Google</name>" +
"<email>no-reply#accounts.google.com</email>" +
"</author>" +
"</entry>" +
"</feed>";
XDocument doc = XDocument.Parse(xml);
XElement firstNode = (XElement)doc.FirstNode;
XNamespace ns = firstNode.Name.Namespace;
var results = doc.Elements(ns + "feed").Select(x => new {
tagline = (string)x.Element(ns + "tagline"),
message_subject = (string)x.Element(ns + "entry").Element(ns + "title")
}).FirstOrDefault();
}
}
}
I have been trying hard to parse an xml string, but all to no avail
<EnquirySingleItemResponse xmlns="http://tempuri.org/"> <EnquirySingleItemResult>
<Response>
<MSG>SUCCESS</MSG>
<INFO>TESTING</INFO>
</Response> </EnquirySingleItemResult> </EnquirySingleItemResponse>
My Code returns null or the texts in the xml tags no matter how i parse it. I have checked some post, but they seem not to be working.
See my code snipped below
XElement anotherUnwrappedResponse = ( from _xml in axdoc.Descendants(tempuri + "EnquirySingleItemResponse")
select _xml).FirstOrDefault();
string response = anotherUnwrappedResponse.Value;
axdoc.Descendants is used because i unwrapped the soap body, to have the xml above
Try this
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
string input =
"<EnquirySingleItemResponse xmlns=\"http://tempuri.org/\">" +
"<EnquirySingleItemResult>" +
"<Response>" +
"<MSG>SUCCESS</MSG>" +
"<INFO>TESTING</INFO>" +
"</Response> </EnquirySingleItemResult> </EnquirySingleItemResponse>";
XDocument doc = XDocument.Parse(input);
string msg = doc.Descendants().Where(x => x.Name.LocalName == "MSG").FirstOrDefault().Value;
string info = doc.Descendants().Where(x => x.Name.LocalName == "INFO").FirstOrDefault().Value;
}
}
}