http://www.dreamincode.net/forums/xml.php?showuser=335389
Given the XML above, how can I iterate through each element inside of the 'lastvisitors' element, given that each child group is the same with just different values?
//Load latest visitors.
var visitorXML = xml.Element("ipb").Element("profile").Element("latestvisitors");
So now I have captured the XElement containing everything I need. Is there a way to loop through the elements to get what I need?
I have this POCO object called Visitor whose only purpose is to hold the necesary information.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace SharpDIC.Entities
{
public class Visitor
{
public string ID { get; set; }
public string Name { get; set; }
public string Url { get; set; }
public string Photo { get; set; }
public string Visited { get; set; }
}
}
Thanks again for the help.
You can probably just do something like this in Linq:
XDocument xml = XDocument.Parse(xmlString);
var visitors = (from visitor in xml.Descendants("latestvisitors").Descendants("user")
select new Visitor() {
ID = visitor.Element("id").Value,
Name = visitor.Element("name").Value,
Url = visitor.Element("url").Value,
Photo = visitor.Element("photo").Value,
Visited = visitor.Element("visited").Value
});
The only caveat here is that I didn't do any null checking.
Just do a linq query to select all the elements as your object.
var visitors = (from v in xml.Element("ipb").Element("profile")
.Element("latestvisitors").Elements()
select new Visitor {
ID = (string)v.Element("id"),
Name = (string)v.Element("name"),
}).ToList();
Related
I've been struggling to find out how to select multiple values from an XML file, compare them to a special value and then do something. So far I just managed to select a single value but I also need a different one in the same select, I hope you can assist me
XML Structure
<?xml version="1.0" encoding="utf-8"?>
<userConnectionSettings version="1" lastApplicationUrl="xxx" lastIdentity="yyy">
<application url="xxx" lastFolderId="zzz">
<user name="test" domain="domain.tld" lastFolderId="yyy" />
</application>
</userConnectionSettings>
Now basically, what i want to do is read the lastApplicationURL and the domain value. I managed to do the lastApplicationURL but i can't seem to select the domain and i don't know how to get that value properly. Here's my code :
XDocument foDoc = XDocument.Load(FrontOfficePath);
foreach (var FOurl in foDoc.Descendants("userConnectionSettings"))
{
string FOappURL = (string)FOurl.Attribute("lastApplicationUrl");
if (FOappURL == "something")
{
TODO
}
else
{
TODO
}
}
You can select domain attribute, in two different ways :
1 - Like #Juharr comment :
foreach (var FOurl in foDoc.Descendants("userConnectionSettings"))
{
string domain = FOurl
.Element("application")
.Element("user")
.Attribute("domain")
.Value;
....
}
Or, by getting descendant of application and select the first item, like :
foreach (var FOurl in foDoc.Descendants("userConnectionSettings"))
{
string domain = FOurl.Descendants("application")
.Select(x => x.Element("user").Attribute("domain").Value)
.FirstOrDefault();
....
}
i hope you find this helpful.
Try following :
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);
List<Application> applications = doc.Descendants("application").Select(x => new Application()
{
url = (string)x.Attribute("url"),
id = (string)x.Attribute("lastFolderId"),
name = (string)x.Element("user").Attribute("name"),
domain = (string)x.Element("user").Attribute("domain"),
folder = (string)x.Element("user").Attribute("lastFolderId")
}).ToList();
}
}
public class Application
{
public string url { get; set; }
public string id { get; set; }
public string name { get; set; }
public string domain { get; set; }
public string folder { get; set; }
}
}
Hi I Have the following XML
<Feed>
<Control>
<Username>Fnol13</Username>
<Password>12345</Password>
</Control>
<Repairer>
<RepairerName>Test</RepairerName>
<RepairerAddress>Test</RepairerAddress>
<RepairerTelephone>Test</RepairerTelephone>
</Repairer>
</Feed>
And a Model Class Containing following Properties
[Serializable]
public class Job {
public string Username { get; set; }
public string Password { get; set; }
public string Reference { get; set; }
public string RepairerAddress { get; set; }
public string RepairerTelephone { get; set; }
}
I am using following Linq Query to Extract Data from XML
var results = from job in xmlDoc.Descendants("Control")
select new Job {
Username = (string)job.Element("Username").Value,
Password = (string)job.Element("Password").Value
};
// Here I want to add as well Descendants("Repairer") using same query
return results.ToList();
Problem is that can return Descendants("Control") However I would like to get also Descendants("Repairer") and return in a same list as my model is showing. Could you please help me to write Linq Query and I am confirming you I am very new in Linq.
Your Model Job looks confusing to me because you may have multiple Control &
Repairer nodes in which case it should map to a collection. Anyways for current XML I assume you need the elements of first Repairer node, you can achieve it like this:-
var results = from job in xmlDoc.Root.Elements("Control")
let Repairer = job.Parent.Elements("Repairer").FirstOrDefault()
select new Job
{
Username = (string)job.Element("Username"),
Password = (string)job.Element("Password"),
RepairerName = (string)Repairer.Element("RepairerName"),
RepairerAddress = (string)Repairer.Element("RepairerAddress"),
RepairerTelephone = (string)Repairer.Element("RepairerTelephone")
};
Also, note (string)job.Element("Username") will give you the node value. There is no need to call the Value property.
Update:
You can use XDocument when working with LINQ-to-XML:-
XDocument xmlDoc = XDocument.Load(XMLpath);
You could do something like -
Reference = (string)job.Parent.Descendants("Repairer").FirstOrDefault().Element("RepairerAddress").Value;
I have profile.xml file in my web folder:
<?xml version="1.0" encoding="utf-8"?>
<myXML>
<RealName>Nguyen Van A</RealName>
<Email>vyclarks#gmail.com</Email>
<Phone>2165421</Phone>
<Address>Ho Chi Minh</Address>
<Link1>dtvt</Link1>
<Link2></Link2>
<Link3></Link3>
</myXML>
As recommended, I get data from that file by code below:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Xml.Linq;
public class profile
{
public string realname { get; set; }
public string email { get; set; }
public string phone { get; set; }
public string address { get; set; }
public string link1 { get; set; }
public string link2 { get; set; }
public string link3 { get; set; }
}
public void getProfile()
{
string path = this.Server.MapPath("~/Lecturer/");
string targetPath = path + #"\"+username+"\\profile.xml";
bool isExists = System.IO.Directory.Exists(targetPath);
if(isExists)
{
List<profile> profiles = (
from e in XDocument.Load(targetPath)
.Root.Element("myXML")
select new profile
{
realname = (string) e.Element("RealName"),
email = (string) e.Element("Email"),
phone = (string) e.Element("Phone"),
address = (string) e.Element("Address"),
link1 = (string) e.Element("Link1"),
link2 = (string) e.Element("Link2"),
link3 = (string) e.Element("Link3")
}
).ToList();
}
...//code to get list value...
}
But it has an error: Cannot resolve symbol "select"
Is there any better way to get data from profile.xml file???
There is an error in your from e in ... statement.
XDocument.Load(targetPath).Root.Element("myXML") returns only one XML element for you. So you can't do linq query against a single object (don't mix with the collections containing only one object).
To get it working you need to change Element method to Elements:
from e in XDocument.Load(targetPath).Root.Elements("myXML")
select new profile
{
realname = (string) e.Element("RealName"),
email = (string) e.Element("Email"),
phone = (string) e.Element("Phone"),
address = (string) e.Element("Address"),
link1 = (string) e.Element("Link1"),
link2 = (string) e.Element("Link2"),
link3 = (string) e.Element("Link3")
}
Update
If you have only one myXML node in XML file (since it's a root node in your example) then you don't need linq query at all. Try to read data from the XML in a next way:
var prof = XDocument.Load(targetPath).Root;
var p = new profile()
{
realname = prof.Element("RealName").Value,
email = prof.Element("Email").Value,
phone = prof.Element("Phone").Value,
address = prof.Element("Address").Value,
link1 = prof.Element("Link1").Value,
link2 = prof.Element("Link2").Value,
link3 = prof.Element("Link3").Value
}
I've tested it with XML from your sample
I have this model:
namespace Model
{
public class Category
{
public int Id { get; set; }
public string Name { get; private set; }
public Category()
{ }
public Category(string name)
{
Name = name;
}
}
}
When I store a document and retrieve it, the result is a list of documents with zero elements:
using (var session = _documentStore.OpenSession())
{
session.Store(category);
session.SaveChanges();
var categories = session.Query<Model.Category>().ToList();
}
category get the proper id, that is "1". But when I do the Query, then I do not get any elements.
If I Load the document:
var category = session.Load<Model.Document>("categories/1")
Instead of the Query, I get the proper category.
How do I load all documents from Raven?
I figured it out:
I have to wait for non staled results. So if I change my Query out with this:
session.Query<Model.Category>().Customize(cr => cr.WaitForNonStaleResults()).ToList();
it works just fine.
I have an XML file
<searchResponse requestID=“500” status=“success”>
<pso>
<psoID ID=“770e8400-e29b-41d4-a716-446655448549”
targetID=“mezeoAccount”/>
<data>
<email>user2#example.net</email>
<quotaMeg>100</quotaMeg>
<quotaUsed>23</quotaUsed>
<realm>Mezeo</realm>
<path>/san1/</path>
<billing>user2</billing>
<active>true</active>
<unlocked>true</unlocked>
<allowPublic>true</allowPublic>
<bandwidthQuota>1000000000</bandwidthQuota>
<billingDay>1</billingDay>
</data>
</pso>
</searchRequest>
and I want to extract the data into a single business object. Am I better to go
MezeoAccount mcspAccount = new MezeoAccount();
mcspAccount.PsoID = doc.Element("psoID").Attribute("ID").Value;
mcspAccount.Email = doc.Element("email").Value;
...
or build a list even though I know there is only 1 record in the file?
var psoQuery = from pso in doc.Descendants("data")
select new MezeoAccount {
PsoID = pso.Parent.Element("psoID").Attribute("ID").Value,
Email = pso.Element("email").Value,
... };
What would people suggest would be the more correct way, or a better way even, if I missed something.
If you know that your xml only will contain one record of the data in mind you shouldn't create a list for it. So your first example looks fine.
A pattern I personally use is something like this:
public class MezeoAccount
{
public string PsoID { get; set; }
public string Email { get; set; }
public static MezeoAccount CreateFromXml(XmlDocument xml)
{
return new MezeoAccount()
{
PsoID = xml.Element("psoID").Attribute("ID").Value,
Email = doc.Element("email").Value;
};
}
}
//Usage
var mezeoAccount = MezeoAccount.CreateFromXml(xml);
It looks like you didn't get a working answer to this question. Assuming that there can only be one account in the XML file, I would do it like this:
using System;
using System.Linq;
using System.Xml.Linq;
public class MezeoAccount
{
public string PsoId { get; set; }
public string Email { get; set; }
public int QuotaMeg { get; set; }
// Other properties...
}
public class Program
{
public static void Main()
{
XDocument doc = XDocument.Load("input.xml");
XElement pso = doc.Element("searchResponse").Element("pso");
XElement data = pso.Element("data");
MezeoAccount x = new MezeoAccount
{
PsoId = pso.Element("psoID").Attribute("ID").Value,
Email = data.Element("email").Value,
QuotaMeg = int.Parse(data.Element("quotaMeg").Value),
// Other properties...
};
}
}