How to lookup predicates from a subject URINode - c#

I don't realy understand how to read te predicates (URINodes) from a subject URINode.
Also, is there anyway to lookup IURINodes by specific predicates values?

I seem to have found my required methode on this post!
My solution:
//Path to RDF/XML file
pathToRDF = "c:\dotNetRdfExample\exampleRDF.xml"
// A IGraph Parser, implementing the IRdfReader interface (So it can work with RDF/XML formats)
RdfXmlParser rxpparser = new RdfXmlParser();
// Create a graph for the rdf data
Graph g = new Graph();
// Makes sure that the XML file is UTF8
StreamReader sr = new StreamReader(pathToRDF, true);
// Fill the empty IGraph, using the rdfXmlParser
rxpparser.Load(g, sr);
// Retrieve all triples with the specified predicate and object
IEnumerable<Triple> specificTriples = g.GetTriplesWithPredicateObject(g.CreateUriNode(UriFactory.Create("http://www.w3.org/1999/02/22-rdf-syntax-ns#type")),
g.CreateUriNode(UriFactory.Create("http://www.projectenportfolio.nl/wiki/index.php/Speciaal:URIResolver/Category-3AHandelingsperspectief")));

Related

Capture Invalid XML

I have the following code I'm using to parse XML strings that contain collections of objects. I want to capture the XML for any given object as a string if I can't parse it. I want store it and analyze it if it won't parse correctly. I would prefer not to make a radical change, but I can't figure out how to grab that part of the XML which is invalid and capture it. xmlReader.ReadOuterXml() throw an exception when it can't parse. Thanks in advance.
// we want to read each canonical item in the report from oracle separately
while (xmlReader.ReadToFollowing(ROOT_ELEMENT))
{
string itemXml = String.Empty;
try
{
// this gives us the whole segment of xml including the root element tag
itemXml = xmlReader.ReadOuterXml();
xmlReader.
T processedItem = default;
processedItem = reportMapper.Mapper(itemXml);
successfulItems.Add(new ProcessingResult<T>()
{
ProcessedItem = processedItem
});
}

Store new XML into existing Project Array with Constructor

I'll try to keep it short and thank you in advance.
I have created a Quiz. The Questions and answers, as well as the integer for the correct answer are done via get and set , into a constructor and then created in another class by just creating an object and giving it the parameters. it looks as follows:
allQuestions = new Question[3];
allQuestions[0] = new Question("Question1", "answer1", "answer2",
"answer3", "answer4", 2);
where 2 is the integer that says answer 2 is the correct one.
i do use this array in almost every function in my code.
Now i decided to get the questions from a XML Document instead of creating them here. I'm a C# beginner so i played around and could not get it working.
my self made xml looks as follows :
<questions>
<question>
<ID>1</ID>
<questiontext>Peter</questiontext>
<answer1>der</answer1>
<answer2>da</answer2>
<answer3>21</answer3>
<answer4>lol</answer4>
</question>
<question>
<ID>2</ID>
<questiontext>Paul</questiontext>
<antwort1>dasistid2</antwort1>
<antwort2>27</antwort2>
<antwort3>37</antwort3>
<antwort4>47</antwort4>
</question>
</questions>
so 2 basic nodes (?)
can you explain me how to read that one and store it into my array so i can still use my e.g. "allQuestions.Question1" ? watched a youtube tutorial, quite a lot, could still not get it working in this project.
using visual studio 2017 , WPF , C#
There are a lot of ways to do what you are trying to do. I'll give you a dirty example of a manual solution, as well as a more automatic one that should work. Note that the automatic version won't use your constructor so unless you have an empty constructor defined it may not work for you.
Manual processing using XML Linq:
public IList<Question> ParseXml(string xmlString)
{
var result = new List<Question>();
var xml = XElement.Parse(xmlString);
var questionNodes = xml.Elements("question");
//If there were no question nodes, return an empty collection
if (questionNodes == null)
return result;
foreach (var questionNode in questionNodes)
{
var idNode = questionNode.Element("ID");
var textNode = questionNode.Element("questiontext");
var ant1Node = questionNode.Element("antwort1");
var ant2Node = questionNode.Element("antwort2");
var ant3Node = questionNode.Element("antwort3");
var ant4Node = questionNode.Element("antwort4");
var question = new Question();
question.Id = Convert.ToInt32(idNode?.Value);
// note the '?.' syntax. This is a dirty way of avoiding NULL
// checks. If textNode is null, it returns null, otherwise it
// returns the textNode.Value property
question.Text = textNode?.Value;
question.AnswerOne = ant1Node?.Value;
question.AnswerTwo = ant2Node?.Value;
question.AnswerThree = ant3Node?.Value;
question.AnswerFour = ant4Node?.Value;
result.Add(question);
}
return result;
}
Next we have the XmlSerializer approach. This is not ideal in all situations, but provides an easy way to serialize objects into XML and to deserialize XML into objects.
public QuestionCollection autoparsexml(string xml)
{
//create the serializer
XmlSerializer serializer = new XmlSerializer(typeof(QuestionCollection));
//convert the string into a memory stream
MemoryStream memStream = new MemoryStream(Encoding.UTF8.GetBytes(xml));
//deserialize the stream into a c# object
QuestionCollection resultingMessage = (QuestionCollection)serializer.Deserialize(memStream);
}
public class QuestionCollection
{
public List<Question> Questions { get; set; }
}

return stored proc results as XML

I am writing an interface in a Web service that returns XML output. The output is queried from a MS SQL database by calling stored procedure. While the stored procedure itself is not returning XML, and I don't have any control over it, I need to return the result set as XML.
Right now, I'm using XmlWriter. Is there an easier, elegant way to do this?
StringBuilder sbLog = new StringBuilder();
XmlWriter writer = XmlWriter.Create(sbLog);
writer.WriteStartDocument();
writer.WriteStartElement("LogSet");
while (sqlRdr.Read())
{
writer.WriteStartElement("Log");
writer.WriteStartElement("EntryID");
writer.WriteValue(sqlRdr["EntryID"].ToString());
writer.WriteEndElement();
//and so on
Yeah XDocument is probably a cleaner interface. It would look something like this:
var doc = new XDocument(
new XElement("LogSet",
new XElement("EntryID", sqlRdr["EntryID"].ToString()),
new XElement(...)
)
);
// save it to a file
doc.Save(pathToDoc);
// or just return it
return doc.ToString();
I'm not sure about it's elegance (I guess that partially depends on how many times you need to reproduce this sort of thing in your code), but I wrote an XmlDataReader a while back that converts tabular data in an IDataReader into an Xml result without the need to create a secondary document/string. As WebServices could return an XmlNode (rather than XmlReader) the second class XmlNodeFactory does that conversion.
Here is the Gist for XmlDataReader
And for the XmlNodeFactory
Usage is something like:
[WebMethod]
public XmlNode GetFeaturedItems(string storeId)
{
var xmlReader = new XmlDataReader(
_db.GetFeaturedItems(storeId),
MyXmlNames.FeaturedItemsNamespace)
{
IncludeIndexField = false,
IncludeResultLevel = false,
RootElementName = "FeaturedItems",
RowElementNameFormat = "Item"
};
return XmlNodeFactory.Create(xmlReader);
}

writing collection of collection to csv file using csv helper

I am using csvhelper to write data to a csv file. I am using C# and VS2010. The object I wish to write is a complex data type object of type Dictionary< long,List<HistoricalProfile>>
Below is the code I am using to write the data to the csv file:
Dictionary<long, List<HistoricalProfile>> profiles
= historicalDataGateway.GetAllProfiles();
var fileName = CSVFileName + ".csv";
CsvWriter writer = new CsvWriter(new StreamWriter(fileName));
foreach (var items in profiles.Values)
{
writer.WriteRecords(items);
}
writer.Dispose();
When it loops the second time I get an error
The header record has already been written. You can't write it more than once.
Can you tell me what I am doing wrong here. My final goal is to have a single csv file with a huge list of records.
Thanks in advance!
Have you seen this library http://www.filehelpers.net/ this makes it very easy to read and write CSV files
Then your code would just be
var profiles = historicalDataGateway.GetAllProfiles(); // should return a list of HistoricalProfile
var engine = new FileHelperEngine<HistoricalProfile>();
// To Write Use:
engine.WriteFile("FileOut.txt", res);
I would go more low-level and iterate through the collections myself:
var fileName = CSVFileName + ".csv";
var writer = new StreamWriter(fileName);
foreach (var items in profiles.Values)
{
writer.WriteLine(/*header goes here, if needed*/);
foreach(var item in items)
{
writer.WriteLine(item.property1 +"," +item.propery2...);
}
}
writer.Close();
If you want to make the routine more useful, you could use reflection to get the list of properties you wish to write out and construct your record from there.

Best practice create a C# Object which reflect and generate a Serialized string

This question may be asked or answered before but I feel that none of the hits really apply.
I would like to create a little class with attributes which will correspond to name and attributes in a output familiar to xml stream. The class should help the program to create a xml-alike string.
string test = "<graph caption='SomeHeader' attribute9='#someotherinfo'>" +
"<set name='2004' value='37800' color='AFD8F8' />" +
"<set name='2005' value='21900' color='F6BD0F' />" +
"<set name='2006' value='32900' color='8BBA00' />" +
"<set name='2007' value='39800' color='FF8E46' />" +
"</graph>";
I think you got the idea. I have a static amount of known attributes which will be used in the tags. The only Tags here is set and graph.
I would like to do something like this,
Helper o = new Helper()
List<Tag> tag = new List<Tag>();
foreach (var someitem in somedatabaseresult)
{
tag.Add(New Graph() { Caption = someitem.field , attribute9 = someitem.otherField });
foreach (var detail in someitem)
{
tag.Add(new Set() { name = detail.Year, value = detail.Value color = detail.Color });
}
}
o.Generate(); // Which will create the structure of result sample above
// and for future extension..
// o.GenerateXml();
// o.GenerateJson();
Please remember that this code is pesudo, just taken from my head. A result of that I have some ideas but it take a day to code and test what best (or whorse).
What would be best practice to solve this task?
[EDIT]
This mysterious "Helper" is the (unluckily typed) class who contains a list of Graph, a list of Set and also (what I think about) contains all available attributes per Graph/Set object. The work of the foreach-loops above are mentioned to fill the Helper class with the data.
[EDIT2]
Result here,
https://gist.github.com/1233331
Why not just create a couple of classes: Graph and Set. Graph would have a property of List<Set>.
In your foreach you can then create an instance or Graph and add instances of Set to its list.
When you're done use the XML Serializer to serialize the Graph object out to XML. Nice and easy to then output to another format as well if your needs change later e.g. serialize to JSON.
Edit following comment:
From top my head so may not be 100% correct...
var myGraph = BuildMeAGraph();
var serializer = new XmlSerializer(typeof(Graph));
var writer = XmlWriter.Create("myfile.xml");
serializer.Serialize(writer, myGraph);
But something like that should write it out to a file. If you want the XML in memory then write it out to an XMLTextWriter based on a memory stream instead and then you can write the contents to a string variable or do whatever you need with it.
If you want to create an XML, out of the object tree, then I think, you could try this:
XDocument doc = new XDocument
(
somedatabaseresult.Select
( someitem =>
new XElement("graph",
new XAttribute("Caption", ""),
new XAttribute("attribute9", "#something"),
someitem.Select
(detail =>
new XElement("Set",
new XAttribute("name", "2003"),
new XAttribute("value", "34784"),
new XAttribute("color", "#003300")
)
)
);
//save to file as XML
doc.Save("output.xml");
//save to local variable as XML string
string test = doc.ToString();
I wrote the save value for tags, as you've used in your code. However, I think you would like this:
new XAttribute("name", detail.name),
new XAttribute("value", detail.value),
new XAttribute("color", detail.color)
Or whatever value you want to give to each attribute from the object detail.
Use the XmlSerializer.
I'd use the ExpandoObject, but I don't see the reason for what you are doing.

Categories