XML sections to DataTable - c#

I have an XML file that I need to load into a DataSet. The file looks like this:
<?xml version="1.0" encoding="utf-8" ?>
<computers>
<computer name="laptop">
<role roleId="1" roleName="Supervisor" />
<role roleId="2" roleName="Psuedo Pilot 1" />
<role roleId="3" roleName="Psuedo Pilot 2" />
</computer>
<computer name="triplescreen">
<role roleId="1" roleName="Tower Controllers" />
<role roleId="2" roleName="Final Controllers" />
<role roleId="3" roleName="Approach/Arrival" />
</computer>
</computers>
I can read it into a DataSet like so:
string xmlFile = string.Format(#"{0}\configuration.xml", Environment.CurrentCirectory);
DataSet xmlDataSet = new DataSet("XML DataSet");
xmlDataSet.ReadXml(xmlFile);
But when I step through the code and look at what tables are in the DataSet, I only computer and role in the Table dropdown:
I am trying to get the DataSet to have a Table for each 'computer' of a specific name.

Related

Iterating through linq results results in more items than query count

I'm fairly new to LINQ but this seemed pretty straightforward.
I have an XML doc which contains a structure like this:
<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<folders>
<folder id="-1" parent="-100">
<name><![CDATA[Root]]></name>
<children>
<folder id="2" parent="-1">
<name><![CDATA[Contribution]]></name>
<documents />
<children>
<folder id="775" parent="2">
<name><![CDATA[category1]]></name>
<documents />
<children>
<folder id="2319" parent="775">
<name><![CDATA[Acad_Depts1]]></name>
<documents />
<children>
<folder id="26965" parent="2319">
<name><![CDATA[Student1]]></name>
<documents>
<document>
</document>
</documents>
</folder
</children>
</folder>
<folder id="2319" parent="775">
<name><![CDATA[Acad_Depts2]]></name>
<documents />
<children>
<folder id="26965" parent="2319">
<name><![CDATA[Student1]]></name>
<documents>
<document>
</document>
</documents>
</folder
</children>
</folder>
etc...
</children>
</folder>
</children>
</folder>
</children>
</folder>
</folders>
What I'm trying to do is to select all the elements with an attribute 'parent="775"'.
XElement xelement = XElement.Load("folders_only_registrar_folder.xml");
IEnumerable <XElement> folders = xelement.Elements();
var query = from node in folders.Descendants("folder")
where node.Attribute("parent").Value == registrarNodeID
select node;
Console.WriteLine(query.Count());
Console.ReadKey();
foreach(XElement departmentNode in query.Descendants("name"))
{
Console.WriteLine(departmentNode.Value.ToString());
}
When I run the query and test the count, I get 48 results (which is good)... but when I try to write out those same nodes, I get hundreds of results. For some reason it's giving me almost ALL of the elements named "folder" including children folders.
Thoughts as to what I'm doing wrong?
UPDATE... ok so now I know why i'm getting all the folders but any thoughts on how to create a collection of each grouping of nodes and sub-nodes?
Can the selection in LINQ send each 775 folder node (plus it's collective sub-nodes) into some sort of collection of nodes and then I could parse through them in a foreach by grouping of node?
Replace query.Descendants() with just query. query.Descendants() gets every child of every node that was originally contained within query.

Generating class file from xsd file using vs command prompt issue

I have this following xsd file. I try to generate class using vs command line like:
command line word = xsd test.xsd /c
"Data at the root level is invalid. Line2, position1.". I am not sure why I having this error. Please advise.
<?xml version="1.0" ?>
- <ResultSet>
- <DataRow>
<Mailing>CCACCL0030</Mailing>
<MailingDesc>'other response' chelsea CA</MailingDesc>
<Notes />
<Direction>O</Direction>
<HistoryOnly>N</HistoryOnly>
</DataRow>
- <DataRow>
<Mailing>VOUCHER20</Mailing>
<MailingDesc>£20 T&M Voucher</MailingDesc>
<Notes />
<Direction>O</Direction>
<HistoryOnly>N</HistoryOnly>
</DataRow>
-
</ResultSet>
To generate code:
Edit the example file so that the & character is encoded. Specifically, swap it for &
Rename the file to test.xml
Run xsd.exe test.xml to generate a schema e.g. test.xsd
Run xsd.exe /c test.xsd to generate classes from the schema
EDIT: This worked fine for me. Here's the contents of my test.xml:
<?xml version="1.0" ?>
<ResultSet>
<DataRow>
<Mailing>CCACCL0030</Mailing>
<MailingDesc>'other response' chelsea CA</MailingDesc>
<Notes />
<Direction>O</Direction>
<HistoryOnly>N</HistoryOnly>
</DataRow>
<DataRow>
<Mailing>VOUCHER20</Mailing>
<MailingDesc>£20 T&M Voucher</MailingDesc>
<Notes />
<Direction>O</Direction>
<HistoryOnly>N</HistoryOnly>
</DataRow>
</ResultSet>

I cannot access XML data using LINQ to XML from a file

I have the following XML in a file called contents.xml that is in the data directory in my C drive:
<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<feed xml:base="http://datacenter1.table.core.windows.net/"
xmlns:d="http://schemas.microsoft.com/ado/2007/08/dataservices"
xmlns:m="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata"
xmlns="http://www.w3.org/2005/Atom">
<title type="text">TestContents</title>
<entry>
<title type="text" />
<author>
<name />
</author>
<link rel="edit" title="TestContents" />
<content type="application/xml">
<m:properties>
<d:PartitionKey>0100000</d:PartitionKey>
<d:Text>xx</d:Text>
</m:properties>
</content>
</entry>
<entry>
<title type="text" />
<updated />
I need to get the values of the <d:Text> so I created this console application:
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
XDocument xmlDoc = XDocument.Load(#"c:\data\contents.xml");
var q = from c in xmlDoc.Descendants("entry")
select (string)c.Element("link") ;
Console.WriteLine(q);
}
}
}
But I have two problems. Firstly the console shows but then disappears before I can see the output. Second if I look at q in the debugger it says "enumeration returns no results".
What's the best way that I can use to get what I really need which is the values of the many <d:Text> ?
First Problem: After Console.WriteLine(q) line, you can write Console.ReadLine(). So result will be displayed until you press "Enter".
Second Problem: If you want all values in a list then u can do q.ToList()

Creating Maintainable Xml structure for a relational database

I am not trying to save dataset or datatable to a xml file rather create a xml structure for saving related data? For example i would like to know how below data could be in a xml file
User
UserId
Username
Password
Roles
RoleId
UserId [FK]
CreatedOn
will it look like this
<User userid="" username="" password="">
<Roles id="">
<Name></Name>
<Description></Description>
</Roles>
</User>
which structure would be best to use xml files as DB
I think you want to consider implementing the XML in a more normalized manner, similar to how you would do so with a relational database. For instance in your current solution you would be required to type out the entire role structure within every user such as
<User userid="1" username="user01" password="password">
<Roles id="1">
<Name>Role 1</Name>
<Description>This is Role 1</Description>
</Roles>
</User>
<User userid="2" username="user02" password="password">
<Roles id="1">
<Name>Role 1</Name>
<Description>This is Role 1</Description>
</Roles>
</User>
A normalized structure could look like the following
<Roles>
<Role id="1">
<Name>Role 1</Name>
<Description>This is Role 1</Description>
</Role>
</Roles>
<User id="1" username="user01" password="password">
<Roles>
<Role>1</Role>
</Roles>
</User>
<User id="2" username="user02" password="password">
<Roles>
<Role>1</Role>
</Roles>
</User>
Hope this helps

creating a "join" to update one XElement from another XElement

I have an XElement, called "XUsers", that will contain XML which looks like this:
<users>
<user id="12345" name="Bob Smith" />
<user id="67890" name="Jamal Stevens" />
<user id="54321" name="Mary Jones" />
</users>
...and another XElement, called "XTasks", that will contain data like this:
<tasks>
<task id="1" title="Task 1" ownerId="54321" />
<task id="2" title="Task 2" ownerId="12345" />
<task id="3" title="Task 3" ownerId="67890" />
</tasks>
I want to add an attribute ("ownerName") to the task elements in the second XElement (XTasks), and set its values according to a "join" with the first XElement (XUsers). So, my final result will be that the XML in XTask will look like this:
<tasks>
<task id="1" title="Task 1" ownerId="54321" ownerName="Mary Jones" />
<task id="2" title="Task 2" ownerId="12345" ownerName="Bob Smith" />
<task id="3" title="Task 3" ownerId="67890" ownerName="Jamal Stevens" />
</tasks>
Is this possible using Linq? I haven't been able to find any examples of this sort of operation on the web. What is the most efficient way to accomplish this in my ASP.NET(C#) code?
Thanks for any advice you can give.
I am doing this without any IDE in front of me, so forgive me for any errors..
foreach (XElement task in XTasks.Elements())
{
XElement userNode = XUsers.Elements().Where(
e => e.Attribute("id").Value == task.Attribute("ownerId").Value).FirstOrDefault();
if (userNode != null)
{
task.Attribute("ownerName").SetValue(userNode.name);
}
}

Categories

Resources