Parsing a Complex XML file with in C# - c#

- <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);
}
}

Related

Update multiple XML nodes that match an Xpath query C#

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 ;
}
}

C# and GDAL implementation - Dataset not returning a value

I am trying to automate a simple geotiff to KMZ process using GDAL and C#
The code I have written is a little buggy and I am having a lot of trouble working out how to use the API. I am hoping this is a simple fix for more advanced developers.
My code is as follows:
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using OSGeo.GDAL;
namespace TestingTIFFtoKMZ
{
class Program
{
static void Main(string[] args)
{
string outputName = Environment.CurrentDirectory + #"\BR01899_Mackenzie_09_EXPORT";
string vrtfile = outputName + #"\tempMosaic.vrt";
string filenameKMZ = outputName + #"\" + Environment.CurrentDirectory + ".kmz";
string[] tiffFiles = Directory.GetFiles(outputName, "*.tif");
var vrtOptions = new GDALBuildVRTOptions(new[] { "-overwrite" });
GDALTranslateOptions transOptions = new GDALTranslateOptions(new[] { "-of", "KMLSUPEROVERLAY", "-co", "format=png" });
Dataset vrtDataset = Gdal.wrapper_GDALBuildVRT_names(vrtfile, tiffFiles, vrtOptions, null, null);
// vrtDataset.Dispose();
Gdal.wrapper_GDALTranslate(filenameKMZ, vrtDataset, transOptions, null, null);
}
}
}
I am getting a null return value on the vrtDataset variable and (i think) as a result, I am getting a null reference error when I call the Gdal.wrapperGDALTranslate method.
Any help appreciated. Thanks.
It needed,
Gdal.AllRegister();
and an absolute path to the VRT file being created.

Reading XML file using Linq

I have tried to read XML file using Linq. Here is my code.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Xml.Linq;
using System.Text;
using System.Threading.Tasks;
namespace LinqToXml
{
class Program
{
static void Main(string[] args)
{
XDocument xdoc = XDocument.Load(#"E:\\XML\\test.xml");
xdoc.Descendants("Title").Select(t => new
{
fontType = t.Attribute("fontType").Value,
fontSize = t.Attribute("fontSize").Value,
fontColor = t.Attribute("fontColor").Value,
Underline = t.Attribute("Underline").Value,
Bold = t.Attribute("Bold").Value,
Italic = t.Attribute("Italic").Value,
}).ToList().ForEach(t =>
{
Console.WriteLine("fontType : " + t.fontType);
Console.WriteLine("fontSize : " + t.fontSize);
Console.WriteLine("fontColor : " + t.fontColor);
Console.WriteLine("Underline : " + t.Underline);
Console.WriteLine("Bold : " + t.Bold);
Console.WriteLine("Italic : " + t.Italic);
});
Console.ReadLine();
}
}
}
It gives an below exception
An unhandled exception of type 'System.NullReferenceException'
occurred in programm.exe
But I can't find what is the error. Can anyone help me..
This is the XML file which I have used..
<Styles>
<Title>
<fontType>TimesNewRoman</fontType>
<fontSize>20</fontSize>
<fontColor>black</fontColor>
<Underline>No</Underline>
<Bold>bold</Bold>
<Italic>No</Italic>
</Title>
<Title>
<fontType>TimesNewRoman</fontType>
<fontSize>20</fontSize>
<fontColor>black</fontColor>
<Underline>No</Underline>
<Bold>bold</Bold>
<Italic>italic</Italic>
</Title>
</Styles>
There aren't any fontType, fontSize, etc. attributes on the Title element. fontType, fontSize are elements, so you should use
fontType = t.Element("fontType").Value

Create Protege Readable Ontology from .NET

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")
});
}
}
}
​

Read from a XML file to an Array node by node in C#

The XML file is like this,There are about 20 Nodes(modules) like this.
<list>
<module code="ECSE502">
<code>ECSE502</code>
<name>Algorithms and Data structures</name>
<semester>1</semester>
<prerequisites>none</prerequisites>
<lslot>0</lslot>
<tslot>1</tslot>
<description>all about algorythms and data structers with totorials and inclass tests</description>
</module>
</list>
I did the following code. But when I debugged it it even didn't went inside to foreach function.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
namespace ModuleEnrolmentCW
{
class XMLRead
{
public string[] writeToXML(string s)
{
string text = s;
string[] arr = new string[6];
XmlDocument xml = new XmlDocument();
xml.Load("modules.xml");
XmlNodeList xnList = xml.SelectNodes("list/module[#code='" + text + "']");
foreach (XmlNode xn in xnList)
{
arr[0] = xn.SelectSingleNode("code").InnerText;
arr[1] = xn.SelectSingleNode("name").InnerText;
arr[2] = xn.SelectSingleNode("semester").InnerText;
arr[3] = xn.SelectSingleNode("prerequisites").InnerText;
arr[4] = xn.SelectSingleNode("lslot").InnerText;
arr[5] = xn.SelectSingleNode("tslot").InnerText;
}
return arr;
}
}
}
Please tell me where is the wrong??
Here is the rest of the code
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace ModuleEnrolmentCW
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
string selected;
private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
XMLRead x = new XMLRead();
selected = (string)listBox1.SelectedItem;
string[] arr2 = x.writeToXML(selected);
label11.Text = arr2[0];
}
}
}
Make sure you are specifying correct path for your xml file.
It is working for me.
This line:
XmlNodeList xnList = xml.SelectNodes("list/module[#code='" + text + "']");
should read:
XmlNodeList xnList = xml.SelectNodes("list/module"); //Does not answer full scope of the question
Edit following a reread of the question:
The OP's code works fine in my tests. Either the file path is not correct, or the the string s passed into text matches the case of the Code value by which you are reading the nodes.
The SelectNodes XPath as you have it is case sensitive.
You appear to be working with XPath V1.0 which doesn't appear to support out of the box case insensitivity if that's a issue. See this link for a way to perform case insensitive XPath searches: http://blogs.msdn.com/b/shjin/archive/2005/07/22/442025.aspx
See also this link: case-insensitive matching in xpath?
Your code is correct, if the input is really the one you shown, and s point to an actual present code. Since you are pointing the file by a relative path, ensure you are loading the file you really expect.
Found the error. I was passing a wrong value to writeToXML method. Insted of passing code, I have passed name

Categories