Retrieving metadata from xml files - c#

I need to retrieve metadata from multiple xml files. The structure of the xml file is the following:
<songs>
<song_title> some title </song_title>
<classification> some classification </classification>
<song_text> some text </song_text>
<singer>
<sing> singer's name </sing>
<gender> gender </gender>
<bornYear> year </bornYear>
<livePlace> live place </livePlace>
<liveArea> live area </liveArea>
</singer>
</songs>
The user chooses the search criteria - live place or live area. Then he enters the name of the place or area, that he searches for. I need to find and display links to songs, which have in its metadata the place or area, that user has entered. I am using .NET 3.5

This answer is more of a pointer...
You can use LINQ to XML to accomplish this task.
What Is LINQ to XML?
LINQ to XML is a LINQ-enabled,
in-memory XML programming interface
that enables you to work with XML from
within the .NET Framework programming
languages.
LINQ to XML is like the Document
Object Model (DOM) in that it brings
the XML document into memory. You can
query and modify the document, and
after you modify it you can save it to
a file or serialize it and send it
over the Internet. However, LINQ to
XML differs from DOM: It provides a
new object model that is lighter
weight and easier to work with, and
that takes advantage of language
improvements in Visual C# 2008.
You can then search and manipulate any XML document element using LINQ query expressions like the following example:
IEnumerable<XElement> partNos =
from item in purchaseOrder.Descendants("Item")
where (int) item.Element("Quantity") *
(decimal) item.Element("USPrice") > 100
orderby (string)item.Element("PartNumber")
select item;

You can use XPathing to easily get whatever you want if you have an aversion to Linq
http://msdn.microsoft.com/en-us/library/ms256086%28VS.85%29.aspx
node.SelectNodes("Songs[/Singer/LivePlace='California']")
this would get all Songs nodes that have a singer node with a liveplace node with the value of California.

Related

How to read an xaml file and get the data I want in C#

I want to read through a xaml file, and find all the lines with 'Annotation.AnnotationText' and get specific data from that line.For example, this line:
<prwab:Branch Condition="{x:Null}" sap2010:Annotation.AnnotationText="testing information " ContinuouslyExecute="False" CreatedBy="System Administrator" CreatedOn="2013-02-23T14:51:28.1555955-05:00" DisplayName="Failure" EnableValidationRule="False" sap:VirtualizedContainerService.HintSize="160,234" ID="ab91dec8-1976-491e-91eb-58e073a69d16" IsReportable="False" LastModifiedBy="System Administrator" LastModifiedOn="2013-02-23T14:51:28.1555955-05:00" MediaRecord="[MediaRecord]" SystemName="CollectDigitsActivity1 Failure6" Timeout="10000" Type="Voice">
I want find all the lines with 'AnnotationText' in my xaml file, and get information like text = 'testing information', id = 'ab91dec8-1976-491e-91eb-58e073a69d16' , created date and lastmodified date.
I have 0 knowledge in this area and I don't know where to start and which method should I use. Thanks for helping!
XAML is just a specific flavour of XML. You will need to use XML parsing to read the file into an object that you can process in this manner. I recommend Linq to XML for this (look at XDocument class to get started), specifically as finding values by XName using a specific namespace as you will need to for the "sap2010" namespace is very easy.
You can then easily parse and extract the information you are looking for using those classes.

How to move through different records in C#

I made a form that basically displays information about different vendors from an XML file that I was given. The XML file is retrieved from a class that I created, called VendorsDB.cs. On my form, I have a Previous and Next button that I want to display the next vendor or the previous vendor (Vendor1, Vendor2...) but I have no idea what method to use. I know I have to use a loop but I'm not sure as to how to code the loop. I've just started programming with C# so I'm really lost. Any help would be greatly appreciated!
Your question is missing alot of information, but what im seeing is that you need to do some work with XML.
I suggest you look into Linq To XML which enabled you to query tags and attributes with query syntax.
A very simple query good look like this:
// Load the xml from the specified path
var xml = XDocument.Load(#"LocationOfXml");
// Query the first element with a "MyXmlTag" as name
var someAttribute = xml.Descendants().FirstOrDefault(x => x.Name == "MyXmlTag");
The query language is far richer than this simple query. Read up and im sure you'll be able to get it working asap.

Pulling out XML values from a string in a database column

I have seen various questions on this, but none seem to be working in my problem. I have an Umbraco site set up and it stores its page contents as XML in a database column. An example one is below:
Sorry for the screen grab and not the actual code, but the editor kept stripping things out.
What I would like to do ideally is either on the page in c#/Linq (have been trying to manipulate is from a string value) or within a SQL query. To be able to pull out the 'url-name', 'nodeName' and 'bodyText' fields.
Many thanks
Since the column is not defined as XML in the database, you can pull out the string and parse the text/string as an XML document:
// xml would be pulled from the DB
string xml = "<RunwayTextpage nodeName=\"Test page\" urlName=\"test-page\"><bodyText>Body Text</bodyText></RunwayTextpage>";
var doc = XDocument.Parse( xml );
string nodeName = doc.Root.Attribute( "nodeName" ).Value;
string urlName = doc.Root.Attribute( "urlName" ).Value;
string bodyText = doc.Root.Element( "bodyText" ).Value;
Another option would be to use string manipulation in the SQL query itself, but that would end up being much less maintainable whereas the above is easily understandable.
Why don't use uQuery? Don't really know your purposes, but it has a method called GetNodesByXPath that gets a collection of nodes or node from an XPath expression by querying the in memory xml cache.
It's more wise in terms of performance if your tree is large.

Generate LINQ to XML C# code, from an XML document?

Does anybody know of a tool that will generate LINQ to XML code from a real XML document or fragment? It's reverse-engineering the common scenario of generating XML.
For example, I want to provide an XML fragment as input like this
<root>
<thing>value</thing>
</root>
and have it generate the equivalent C# LINQ to XML code snippet like so
var x = new XElement("root",
new XElement("thing", new XText("value"));
);
Although I'm looking for a quickie, I'm sure some enterprising individuals will tell me to roll my own and provide some awesome reference code.
See this tool.
the application supports :
XDocument
XDeclaration
XProcessingInstruction
XComment
XNamespace
XElement
XAttribute
generation of business objects
generation of code Linq To Xml (with variables, in method, extraction of
the code corresponding to the selected
nodes)
you can open a Xml file or directly copy to stick xml in the richtextbox
the editor allows to create Xml documents from scratch or to
add/modify existing Xml documents
the editor has several views which are synchronized (Text, treeview)
a help with the seizure (auto completion tags and attributes and
checking in the course of the good
formation of xml) for the text view,…
you can also post the data of the nodes selected in a datagridview
etc
This wouldn't be hard to do using T4 templates, or an XSL transform for that matter, but I don't know anyone who's done it.

Add unique node names from a xml file into a list using c#

Im using c# .net windows form application. I have a xml file. It has many nodes in it. Some nodes are repeated. I want to dispplay the nodes in a tree view. also I don't want repeated nodes.I need unique node name.
For example my xml file is this:
-
-
abc
hello how ru
-
def
i m fine
-
ghi
how abt u
Now I want to display only:
languages
language
key
value
Similarly if there are any other unique nodes, I need to display them.
Please help me.
You can look at Linq2XML.

Categories

Resources