I am trying get data from the xml. Below is the code which
gets data from the XDocument and return list<t>.
However, p.Element("Sponsor") can sometimes be null. How can I check for the null values
var atClauseList = doc.Descendants(CLAUSE_GROUP_TAG).Descendants(AT_CLAUSE_TAG).Select(p => new AtClause()
{
ClauseNumber = (string)p.Element("Number"),
Sponsors = p.Element("Sponsor").Elements(SPONSOR_TAG).Select(y => y.Value)
.ToList(),
Page = p.Element("Sponsor").Element("aItem").Element("AmendText").Element("Page").ElementValueNull(),
Line = p.Element("Sponsor").Element("aItem").Element("AmendText").Element("Line").ElementValueNull(),
LineText = p.Element("Sponsor").Element("aItem").Element("AmendText").Nodes().OfType<XText>().FirstOrDefault().XTextValueNull(),
ItalicText = p.Element("Sponsor").Element("aItem").Element("AmendText").Element("Italic").ElementValueNull(),
ParaList = p.Element("Sponsor").Element("aItem").Element("AmendText").Elements("Para").Select(L => new Para
{
ParaText = (string)L,
Number = ((System.Xml.Linq.XElement)(L)).AttributeValueNull("Number"),
Quote = ((System.Xml.Linq.XElement)(L)).AttributeValueNull("Quote"),
}
).ToList()
}).ToList();
move your code out of an object initializer, and add some logic to it:
var atClauseList = new List<AtClause>();
foreach(var item in doc.Descendants(CLAUSE_GROUP_TAG).Descendants(AT_CLAUSE_TAG))
{
var atClause = new AtClause();
atClause.ClauseNumber = (string)item.Element("Number");
var sponsor = item.Element("Sponsor");
if (sponsor != null)
{
atClause.Sponsors = sponsor.Elements(SPONSOR_TAG).Select(y => y.Value).ToList();
atClause.Page = sponsor.Element("aItem").Element("AmendText").Element("Page").ElementValueNull();
atClause.Line = sponsor.Element("aItem").Element("AmendText").Element("Line").ElementValueNull();
atClause.LineText = sponsor.Element("aItem").Element("AmendText").Nodes().OfType<XText>().FirstOrDefault().XTextValueNull();
atClause.ItalicText = sponsor.Element("aItem").Element("AmendText").Element("Italic").ElementValueNull();
atClause.ParaList = sponsor.Element("aItem").Element("AmendText").Elements("Para").Select(L => new Para
{
ParaText = (string)L,
Number = ((System.Xml.Linq.XElement)(L)).AttributeValueNull("Number"),
Quote = ((System.Xml.Linq.XElement)(L)).AttributeValueNull("Quote"),
}).ToList();
atClauseList.Add(atClause);
}
You can use sequences rather than leaving the IEnumerable immediately:
var value = (string)p.Elements("Sponsor")
.Elements("aItem")
.Elements("AmendText")
.Elements("Page")
.SingleOrDefault()
Related
I have a ControlMeasure table that holds information on each control measure and a ControlMeasurepeopleExposed Table that holds a record for each person exposed in the control measure this could be 1 record or many records.
I Have a controller that populates a List view
For each item in the list, Control Measure, I would like to create a string that shows all the People at risk
e.g.
PeopleString = "Employees, Public, Others";
Ive added a foreach in the controller to show what I'm trying to do however I'm aware that this wont work.
The controller is this:
public ActionResult ControlMeasureList(int raId)
{
//Populate the list
var hazards = new List<Hazard>(db.Hazards);
var controlMeasures = new List<ControlMeasure>(db.ControlMeasures).Where(x => x.RiskAssessmentId == raId);
var cmcombined = (
from g in hazards
join f in controlMeasures
on new { g.HazardId } equals new { f.HazardId }
select new CMCombined
{
Activity = f.Activity,
ControlMeasureId = f.ControlMeasureId,
ExistingMeasure = f.ExistingMeasure,
HazardName = g.Name,
LikelihoodId = f.LikelihoodId,
Rating = f.Rating,
RiskAssessmentId = f.RiskAssessmentId,
SeverityId = f.SeverityId,
}).OrderBy(x => x.Activity).ToList();
var cmPeopleExp = new List<ControlMeasurePeopleExposed>(db.ControlMeasurePeopleExposeds).Where(x => x.RiskAssessmentId == raId);
var peopleExp = from c in cmPeopleExp
join d in db.PeopleExposeds
on c.PeopleExposedId equals d.PeopleExposedId
orderby d.Name
select new RAPeopleExp
{
RAPeopleExpId = c.PeopleExposedId,
PeopleExpId = c.PeopleExposedId,
PeopleExpName = d.Name,
RiskAssessmentId = c.RiskAssessmentId,
ControlMeasureId = c.ControlMeasureId
};
var model = cmcombined.Select(t => new FullControlMeasureListViewModel
{
ControlMeasureId = t.ControlMeasureId,
HazardName = t.HazardName,
LikelihoodId = t.LikelihoodId,
Rating = t.Rating,
SeverityId = t.SeverityId,
Activity = t.Activity,
ExCM = t.ExistingMeasure,
//This section here is where I'm struggling
var PeopleString = new StringBuilder();
foreach (var p in peopleExp)
{
PeopleString.AppendLine(p.PeopleName);
{
PeopleExposed = PeopleString,
});
return PartialView("_ControlMeasureList", model);
}
I know I cant directly put this code in the controller but it does represent what I want to do.
You can't foreach within an object initializer (which is what you're trying to do when instantiating FullControlMeasureListViewModel). You can, however, use a combination of string.Join and peopleExp.Select:
var model = cmcombined.Select(t => new FullControlMeasureListViewModel
{
//other props
PeopleExposed = string.Join(",", peopleExp
.Where(p => p.ControlMeasureId == t.ControlMeasureId)
.Select(p => p.PeopleExpName));
//other props
});
I am parsing XML to create an object. I am just unsure as to how to cast the var lotConfig into a LotCOnfig object.
I have tried :
var lotConfig = (LotConfig) xml.Descendants("LOT_CONFIGURATON").Select(
and
return lotConfig as LotConfig
But None have worked so far.
public LotConfig GetLotConfigData(FileInfo lotFile)
{
var xml = XElement.Load(lotFile.FullName);
var lotConfig = xml.Descendants("LOT_CONFIGURATON").Select(
lot => new LotConfig
{
LotNumber = (string) lot.Element("LOT_NUMBER").Value,
LotPartDescription = lot.Element("PART_DESCRIPTION").Value,
LotDate = lot.Element("LOT_DATE").Value,
ComponentList = lot.Descendants("ASSY_COMPONENT").Select(ac => new LotComponent
{
PartNumber = ac.Descendants("COMPONENT_PART_NUMBER").FirstOrDefault().Value,
ArtworkNumber = ac.Descendants("ARTWORK_NUMBER").FirstOrDefault().Value,
RefDesignator = ac.Descendants("REFERENCE_DESIGNATOR").FirstOrDefault().Value
}).ToList()
});
return lotConfig as LotConfig;
}
Currently lotConfig is of type IEnumerable<LotConfig>, you need to call .FirstOrDefault() at the end of your LINQ to make it return single LotConfig type :
var lotConfig = xml.Descendants("LOT_CONFIGURATON")
.Select(
lot => new LotConfig
{
LotNumber = (string) lot.Element("LOT_NUMBER").Value,
LotPartDescription = lot.Element("PART_DESCRIPTION").Value,
LotDate = lot.Element("LOT_DATE").Value,
ComponentList = lot.Descendants("ASSY_COMPONENT").Select(ac => new LotComponent
{
PartNumber = ac.Descendants("COMPONENT_PART_NUMBER").FirstOrDefault().Value,
ArtworkNumber = ac.Descendants("ARTWORK_NUMBER").FirstOrDefault().Value,
RefDesignator = ac.Descendants("REFERENCE_DESIGNATOR").FirstOrDefault().Value
}).ToList()
})
.FirstOrDefault();
How would I go about getting the ID information using Linq. I'm trying to add them to an array of int.
<FactionAttributes>
<name>Player</name>
<id>0</id>
<relationModifier>1</relationModifier>
<relations>
<id0>100</id0>
<id1>50</id1>
<id2>50</id2>
<id3>50</id3>
<id4>50</id4>
<id5>50</id5>
</relations>
</FactionAttributes>
That is my XML.
Here is the code I'm using so far.
void InitFactions()
{
int count = 0;
string filepath = Application.dataPath + "/Resources/factiondata.xml";
XDocument factionXML = XDocument.Load(filepath);
var factionNames = from factionName in factionXML.Root.Elements("FactionAttributes")
select new {
factionName_XML = (string)factionName.Element("name"),
factionID_XML = (int)factionName.Element("id"),
factionRelations_XML = factionName.Element("relations")// Need to turn this into array.
};
foreach ( var factionName in factionNames)
++count;
foreach ( var factionName in factionNames)
{
Factions f = new Factions();
f.otherFactionsName = new string[count];
f.otherFactionsRelation = new int[count];
int others = 0;
f.FactionName = factionName.factionName_XML;
Debug.Log(factionName.factionRelations_XML);
// Adds Rivals, not self to other list.
foreach (var factionName2 in factionNames)
{
if (factionName.factionID_XML == factionName2.factionID_XML)
continue;
f.otherFactionsName[(int)factionName2.factionID_XML] = factionName2.factionName_XML;
// THIS IS WHERE IM ADDING THE RELATIONS IN //
f.otherFactionsRelation[(int)factionName2.factionID_XML] = factionName.factionRelations_XML[(int)factionName2.factionID_XML];
Debug.Log(f.FactionName + " adds: " + factionName2.factionName_XML);
++others;
}
}
}
I have made multiple attempts using nodes and what not. I can't seem to figure out the correct syntax.
XDocument doc = XDocument.Load(Path);
//To get <id>
var MyIds = doc.Element("FactionAttributes").Element("id").Value;
//To get <id0>, <id1>, etc.
var result = doc.Element("FactionAttributes")
.Element("relations")
.Elements()
.Where(E => E.Name.ToString().Contains("id"))
.Select(E => new { IdName = E.Name, Value = E.Value});
If you want array of ints replace the select with this
.Select(E => Convert.ToInt32(E.Value)).ToArray();
If you are just after the relations Ids use this simple query
var doc = XDocument.Load("c:\\tmp\\test.xml");
var ids = doc.Descendants("relations").Elements().Select(x => x.Value);
If you want the Id and the relations ids in one array use this
var id = doc.Descendants("id").Select(x=>x.Value).Concat(doc.Descendants("relations").Elements().Select(x => x.Value));
My code :
var myList = xDoc.Descendants("localita").Select(n => new
{
ID = n.Element("id").Value.ToString(),
Localita = n.Element("nome").Value.ToString(),
Lat = n.Element("lat").Value.ToString(),
Lng = n.Element("lon").Value.ToString(),
MeteoOggi = new MeteoGiorno()
{
Min = n.Descendants("previsione").First().Element("temp_perc").Value.ToString(),
Max = n.Descendants("previsione").First().Element("temp").Value.ToString(),
DescrizioneTempo = n.Descendants("previsione").First().Element("desc_tempo").Value.ToString(),
Precipitazioni = n.Descendants("previsione").First().Element("prec").Value.ToString(),
VentoDirezione = n.Descendants("previsione").First().Element("v_dir").Value.ToString(),
VentoIntensita = n.Descendants("previsione").First().Element("v_int").Value.ToString(),
Pressione = n.Descendants("previsione").First().Element("press").Value.ToString(),
ZeroTermico = n.Descendants("previsione").First().Element("zerot").Value.ToString(),
Immagine = n.Descendants("previsione").First().Element("id_tempo").Value.ToString()
}
});
but as you can see, that n.Descendants("previsione").First() is "searched" each time when I set the values for the Class MeteoGiorno. Can I put a sort of reference to that node in my example?
Sure you can, just change the Select:
var myList = xDoc.Descendants("localita").Select(n => {
var previsione = n.Descendants("previsione").First();
return new {
ID = n.Element("id").Value.ToString(),
....
MeteoOggi = new MeteoGiorno()
{
Min = previsione.Element("temp_perc").Value.ToString(),
Max = previsione.Element("temp").Value.ToString(),
....
}
}
});
Is there anyway to read the specific line in this xml?
http://i.stack.imgur.com/hDPIg.jpg
var guide = from query in dataFeed.Descendants("MaxPayne3")
select new NewGamesClass
{
GameTitle = (string)query.Element("Title"),
Gamedescription = (string)query.Element("Description"),
GameGuide = (string)query.Element("Guide")
};
GuidesListBox.ItemsSource = guide.Where(ngc => ngc.GameGuide.StartsWith("See 'Payne In The Ass'")).Take(1);
this will show all guides in the xml.
This Work:
var guide = from query in dataFeed.Descendants("MaxPayne3")
select new NewGamesClass
{
GameTitle = (string)query.Element("Title"),
Gamedescription = (string)query.Element("Description"),
GameGuide = (string)query.Element("Guide")
};
//GuidesListBox.ItemsSource = guide.Where(ngc => ngc.GameGuide.StartsWith("See 'Payne In The Ass'")).Take(1);
GuidesListBox.ItemsSource = guide.Where(ngc => ngc.GameTitle.StartsWith("Serious"));
So start with whatever is the first child in the XML.
Just continue your statement with a where clause: (change the condition based on your needs)
var guides = from query in dataFeed.Descendants("MaxPayne3")
select new NewGamesClass
{
GameGuide = (string)query.Element("Guide")
};
AchivementsListBox.ItemsSource = guides.Where( ngc => ngc.GameGuide.StartsWith("Chapter 4:"));