I already wrote a code to deserialize an xml file with root element and elemnts only, the problem that i need it to deserialize a XML file that contains root element, elements and CHILD element.
The file looks like this:
<Claim>
<ClaimNo>LL0000110262</ClaimNo>
<PolicyNo>LP0000004481</PolicyNo>
<PolicyHolder>EST Boras AB</PolicyHolder>
<Locations>
<Location>
<AddressId>1</AddressId>
<Address1>Example Street 1</Address1>
<Addresstype>LocationOfLoss</Addresstype>
<City>Helsinki</City>
<Country>FI</Country>
<Postalzone>12345</Postalzone>
</Location>
</Locations>
<UWYear>2015</UWYear>
<PeriodStart>2015-01-01</PeriodStart>
<PeriodEnd>2015-12-31</PeriodEnd>
<DateOfLoss>2015-07-15</DateOfLoss>
<ReportDate></ReportDate>
<StatusAsPer>2015-12-31</StatusAsPer>
<Coverage>?</Coverage>
<TypeOfLoss>Leakage</TypeOfLoss>
<OriginalCurrency>EUR</OriginalCurrency>
<PaymentCurrency>EUR</PaymentCurrency>
<TotalReservesOrigCurr>0.00</TotalReservesOrigCurr>
<TotalPaymentOrigCurr>0.00</TotalPaymentOrigCurr>
<DeductibleOrigCurr>85000.00</DeductibleOrigCurr>
<TotalAmountOfClaimOrigCurr>3680.00</TotalAmountOfClaimOrigCurr>
<Status>Active</Status>
While my method looks like this:
public async Task<IActionResult> XmlPage(IFormFile xmlFile)
{
var uploads = hostingEnvironment.WebRootPath;
var filePath = Path.Combine(uploads, xmlFile.FileName).ToString();
if (xmlFile.ContentType.Equals("application/xml") || xmlFile.ContentType.Equals("text/xml"))
{
try
{
using (var fileStream = new FileStream(filePath, FileMode.Create))
{
await xmlFile.CopyToAsync(fileStream);
fileStream.Dispose();
XDocument xDoc = XDocument.Load(filePath);
List<DmgRegisterVM> dmgRegistterList = GetDmgFromXml(xDoc);
context.SaveXmlToDb(dmgRegistterList);
}
}
// returning at httpGet with a temp message that says att uploadin is failed
catch
{
ViewBag.Error = "Converting fail";
}
}
// returning at httpGet with a temp message that says att uploadin is failed
else
{
ViewBag.Error = "Uploading fail";
}
return View("Index");
}
private List<DmgRegisterVM> GetDmgFromXml(XDocument xDoc)
{
var list = xDoc.Descendants("Claim").Select(dmgReg =>
new DmgRegisterVM
{
Uwyear = dmgReg.Element("UWYear").Value,
ClaimNo = dmgReg.Element("ClaimNo").Value,
PolicyNo = dmgReg.Element("PolicyNo").Value,
PolicyName = dmgReg.Element("PolicyHolder").Value,
Address1 = dmgReg.Element("Address1").Value,
Address2 = dmgReg.Element("Addresstype").Value,
PostalLocation = dmgReg.Element("City").Value,
Country = dmgReg.Element("Country").Value,
PostalCode = dmgReg.Element("Postalzone").Value
}).ToList();
return list;
}
The question is how do get this child elemnt into my list (deserialized)
<Locations>
<Location>
<AddressId>1</AddressId>
<Address1>Example Street 1</Address1>
<Addresstype>LocationOfLoss</Addresstype>
<City>Helsinki</City>
<Country>FI</Country>
<Postalzone>12345</Postalzone>
</Location>
</Locations>
Assuming you know there will only be one location, then you can do something like this:
var viewModels =
from claim in doc.Descendants("Claim")
from location in claim.Descendants("Location")
select new DmgRegisterVM
{
Uwyear = (string) claim.Element("UWYear"),
ClaimNo = (string) claim.Element("ClaimNo"),
PolicyNo = (string) claim.Element("PolicyNo"),
PolicyName = (string) claim.Element("PolicyHolder"),
Address1 = (string) location.Element("Address1"),
Address2 = (string) location.Element("Addresstype"),
PostalLocation = (string) location.Element("City"),
Country = (string) location.Element("Country"),
PostalCode = (string) location.Element("Postalzone")
};
Related
Im working on getting some values from an RSS feed but i am having difficulties getting a value which has the namespace in the element tag. I've tried adding the namespace to the lookup of the value but i always get null
Any idea on how this is achieved?
Feed
https://wegotthiscovered.com/movies/feed/
Element
xmlns:content="http://purl.org/rss/1.0/modules/content/"
Namespace
content:encoded
public async Task<bool> GetNewsFeeds()
{
Database db = new Database();
Dictionary<string, string> dictionary = new Dictionary<string, string>();
dictionary.Add("https://wegotthiscovered.com/movies/feed/", "Movie");
dictionary.Add("https://wegotthiscovered.com/blu-ray/feed/", "Blu-ray");
dictionary.Add("https://wegotthiscovered.com/reviews/feed/", "Reviews");
dictionary.Add("https://wegotthiscovered.com/featured/feed/", "Featured");
dictionary.Add("https://wegotthiscovered.com/galleries/feed/", "Galleries");
db.DeletMovieNews();
foreach (var pair in dictionary.ToList())
{
try
{
if (PhysicalDevice.HasInternetConnection())
{
XDocument doc = XDocument.Load(pair.Key);
XNamespace nsSys = "http://purl.org/rss/1.0/modules/content/";
var entries = (from item in doc.Descendants("item")
select new Movie_News
{
Content = item.Element(nsSys + "encoded").Value, // ISSUE HERE
Link = item.Element("link").Value,
PublishedDate = item.Element("pubDate").Value,
Title = item.Element("title").Value,
Description = item.Element("description").Value,
GroupName = "News",
FeedName = pair.Value
});
List<Movie_News> newsCollection = entries.ToList();
if (newsCollection.Count() != 0)
{
using (var rateGate = new RateGate(40, TimeSpan.FromSeconds(10)))
{
rateGate.WaitToProceed();
foreach (Movie_News item in newsCollection)
{
string regex = #"((http|ftp|https):\/\/)?([\w_-]+(?:(?:\.[\w_-]+)+))([\w.,#?^=%&:\/~+#-]*[\w#?^=%&\/~+#-])?";
Match match = Regex.Match(item.Description, regex);
if (match.Success)
{
item.ImageUrl = match.Value;
item.B64Image = await DownloadImage(item.ImageUrl);
}
item.Description = item.Description.Remove(0, item.Description.IndexOf("</div>"));
item.Description = item.Description.Replace("</div>","");
db.InsertNewsData(item);
}
}
}
return true;
}
}
catch(Exception ex)
{
return false;
}
}
return true;
}
}
Typical , soon as i completed the write up, its working now
Based on the index in the controller, how would I append a record to an XML file? I've done some research, but I can't seem to wrap my head around it.
Index(controller)
public ActionResult Index(string sortOrder)
{
XmlDocument doc = new XmlDocument();
doc.Load("C:\\Users\\Matt.Dodson\\Desktop\\SampleWork\\PersonsApplicationFromXMLFile\\PersonsApplicationFromXMLFile\\DAL\\Personal.xml");
IEnumerable<Personal> persons = doc.SelectNodes("/Persons/record").Cast<XmlNode>().Select(node => new Personal()
{
ID = node["ID"].InnerText,
Name = node["Name"].InnerText,
Email = node["Email"].InnerText,
DateOfBirth = node["DateOfBirth"].InnerText,
Gender = node["Gender"].InnerText,
City = node["City"].InnerText
});
switch (sortOrder)
{
case "ID":
persons = persons.OrderBy(Personal => Personal.ID);
break;
case "Name":
persons = persons.OrderBy(Personal => Personal.Name);
break;
case "City":
persons = persons.OrderBy(Personal => Personal.City);
break;
default:
break;
}
return View(persons.ToList());
}
What I've tried:
Create(Controller)
[HttpPost]
public ActionResult Create(FormCollection collection)
{
string xmlFile = "C:\\Users\\Matt.Dodson\\Desktop\\SampleWork\\PersonsApplicationFromXMLFile\\PersonsApplicationFromXMLFile\\DAL\\Personal.xml";
try
{
XmlDocument doc = new XmlDocument();
doc.Load(xmlFile);
IEnumerable<Personal> persons = doc.SelectNodes("/Persons/record")
.Cast<XmlNode>()
.Select(node => new Personal()
{
ID = node["ID"].InnerText,
Name = node["Name"].InnerText,
Email = node["Email"].InnerText,
DateOfBirth = node["DateOfBirth"].InnerText,
Gender = node["Gender"].InnerText,
City = node["City"].InnerText
});
persons.appendTo(xmlFile);
return RedirectToAction("Index");
}
catch
{
return View();
}
}
I am bad at syntax so this is probably all wrong.
For starters that are a few classes available in C#, suing the specific class that you are using, you should be able to do the following
XDocument doc = XDocument.Load(xmlFile);
var parent = doc.Descendants("NameOfParentTag").FirstOrDefault();//if there is only one
parent.Add(new XElement("personal",
new XElement("ID", ID_Value),
new XElement("Name" = Name_Value),
new XElement("Email", Email_value)));
doc.Save(xmlFile);
That will append the value to your xmlFile, the NameOfParentTag is the name of the parentLocation where you want to insert the value, in your case I would assume that would be record
The second way would be
doc.Load(xmlFile);
XmlNode editNode = doc.SelectSingleNode("targetNode");
XmlNode node = nodes[0];//get the specific node, not sure about your xml structure
XmlElement elem = node.OwnerDocument.CreateElement("personal");
elem.InnerXml = personal.SerializeAsXml();
node.AppendChild(elem);
where the SerializeAsXml method looks like
public static string SerializeAsXml(this Personal personal)
{
var emptyNamepsaces = new XmlSerializerNamespaces(new[] { XmlQualifiedName.Empty });
var settings = new XmlWriterSettings();
settings.Indent = true;
settings.OmitXmlDeclaration = true;
//serialize the binding
string xmlOutput = string.Empty;
using (StringWriter stream = new StringWriter())
using (XmlWriter xmlWriter = XmlWriter.Create(stream, settings))
{
XmlSerializer serializer = new XmlSerializer(personal.GetType());
serializer.Serialize(xmlWriter, obj, emptyNamepsaces);
xmlOutput = stream.ToString();
}
return xmlOutput;
}
NB, for the above method you would need to decorate your XML with the necessary attributes
I am parsing an xml file using the following which works well but my question is how do I get the value from the tag <themename> because I am using oXmlNodeList for my loop. Just don't want to be writing a separate routine for one tag.
XmlDocument xDoc = new XmlDocument();
List<active_theme> listActiveTheme = new List<active_theme>();
string path = AppDomain.CurrentDomain.BaseDirectory + #"themes\test\configuration.xml";
xDoc.Load(AppDomain.CurrentDomain.BaseDirectory + #"themes\test\configuration.xml");
//select a list of Nodes matching xpath expression
XmlNodeList oXmlNodeList = xDoc.SelectNodes("configuration/masterpages/masterpage");
Guid newGuid = Guid.NewGuid();
foreach (XmlNode x in oXmlNodeList)
{
active_theme activetheme = new active_theme();
string themepage = x.Attributes["page"].Value;
string masterpage = x.Attributes["name"].Value;
activetheme.active_theme_id = newGuid;
activetheme.theme = "Dark";
activetheme.page = themepage;
portalContext.AddToActiveTheme(activetheme);
}
portalContext.SaveChanges();
XML File for theme configuration
<configuration>
<themename>Dark Theme</themename>
<masterpages>
<masterpage page="Contact" name="default.master"></masterpage>
<masterpage page="Default" name="default.master"></masterpage>
<masterpage page="Blue" name="blue.master"></masterpage>
<masterpage page="red" name="red.master"></masterpage>
</masterpages>
</configuration>
I'd use LINQ to XML to start with. Something like this:
var doc = XDocument.Load(...);
var themeName = doc.Root.Element("themename").Value;
Guid themeGuid = Guid.NewGuid();
foreach (var element in doc.Root.Element("masterpages").Elements("masterpage"))
{
ActiveTheme theme = new ActiveTheme
{
ThemeName = themeName,
ActiveThemeId = themeGuid,
Page = element.Attribute("page").Value,
MasterPage = element.Attribute("name").Value
};
portalContent.AddToActiveTheme(theme);
}
portalContext.SaveChanges();
Hello in a Windows Phone 8 app I have run into a problem, with deserializing xml from a webclient download.. I wait on the DownloadStringCompletedEvent and then I use the folowing code.
private void PopularDownloaded(object sender, DownloadStringCompletedEventArgs e)
{
if (e.Result == null || e.Error != null)
{
MessageBox.Show("There was an error connecting to the server");
}
else
{
//MessageBox.Show(e.Result.ToString());
XDocument loadedpopData = XDocument.Load(e.Result);
var popdata = from query in loadedpopData.Descendants("pattern")
select new poppatterns
{
Title = (string)query.Element("title"),
UserName = (string)query.Element("userName"),
DateCreated = (string)query.Element("dateCreated"),
ImageUrl = (string)query.Element("imageUrl"),
ApiUrl = (string)query.Element("apiUrl"),
};
poplonglist.ItemsSource = popdata.ToList();
}
}
but it throws the following exception:
An unhandled exception of type 'System.Reflection.TargetInvocationException' occurred in System.Windows.ni.dll
If I but a local xml file in the project and then load that like this
XDocument loadedpopData = XDocument.Load("Resources/top.xml");
it loads fine so it must be something with the
XDocument loadedpopData = XDocument.Load(e.Result);
I tried looking at the e.Result and it is the correct data.
Any ideas what I'm doing wrong??
In e.Result you have an XML string, but XDocument.Load(string) is for load data from file, and string argument is path to file, you should use XDocument.Parse(e.Result) to load XDocument from XML string data.
Post in MSDN about Load: XDocument.Load()
Post in MSDN about Parse: XDocument.Parse()
Change this line
//MessageBox.Show(e.Result.ToString());
XDocument loadedpopData = XDocument.Load(e.Result);
to
//MessageBox.Show(e.Result.ToString());
XDocument loadedpopData = XDocument.Parse(e.Result);
quick cooking sample. and result as well, from LinqPad
code:
var xml = new XElement("root");
for (var i = 0; i <10; i++)
{
xml.Add(new XElement("pattern", new XElement("title", "title" + i.ToString()),
new XElement("title", "title" + i.ToString()),
new XElement("userName", "userName" + i.ToString()),
new XElement("dateCreated", "dateCreated" + i.ToString()),
new XElement("imageUrl", "ImageUrl" + i.ToString()),
new XElement("apiUrl", "ApiUrl" + i.ToString())));
}
var xmlString = xml.ToString();
var loadedpopData = XDocument.Parse(xmlString);
var popdata = from query in loadedpopData.Descendants("pattern")
select new {
Title = (string)query.Element("title"),
UserName = (string)query.Element("userName"),
DateCreated = (string)query.Element("dateCreated"),
ImageUrl = (string)query.Element("imageUrl"),
ApiUrl = (string)query.Element("apiUrl"),
};
popdata.Dump();
Result:
Title UserName DateCreated ImageUrl ApiUrl
title0 userName0 dateCreated0 ImageUrl0 ApiUrl0
title1 userName1 dateCreated1 ImageUrl1 ApiUrl1
title2 userName2 dateCreated2 ImageUrl2 ApiUrl2
title3 userName3 dateCreated3 ImageUrl3 ApiUrl3
title4 userName4 dateCreated4 ImageUrl4 ApiUrl4
title5 userName5 dateCreated5 ImageUrl5 ApiUrl5
title6 userName6 dateCreated6 ImageUrl6 ApiUrl6
title7 userName7 dateCreated7 ImageUrl7 ApiUrl7
title8 userName8 dateCreated8 ImageUrl8 ApiUrl8
title9 userName9 dateCreated9 ImageUrl9 ApiUrl9
I have a xml file that looks like
<Name>AAA</Name>
<Age>23</Age>
<I1>
<Element1>A</Element1>
<Element2>B</Element2>
<Element3>C</Element3>
<\I1>
<I2>
<Element1>AA</Element1>
<Element2>BB</Element2>
<Element3>CC</Element3>
</I2>
I am reading all the values of elements using xmlreader in C# 3.0. But now i have to change by reading only the values within particular start and end tage. For the xml file mentioned above, i need to read <Name>, <Age> by default and then i have a function that returns the value "I1" or "I2" which is basically the element names. If it returns "I1" then i should read only the elements between <I1> and </I1> and should not read <I2> and vice versa. So the code structure would be (just the logic please ignore the syntax errors) like
/******function that returns element name I1 or I2*********/
string elementName = func(a,b);
xmlreader reader = reader.create("test.xml");
while(reader.read())
{
switch(reader.nodetype)
{
case xmlnodetype.element:
string nodeName = reader.name
break;
case xmlnodetype.text
switch(nodeName)
{
/*************Read default name and age values*******/
case "Name":
string personName = reader.value
break;
case "Age":
string personAge = reader.value;
break;
/*******End of reading default values**************/
/*** read only elements between the value returned by function name above
If function returns I1 then i need to read only values between <I1> </I1> else read </I2> and </I2>**/
}
}
}
Thanks!
So assuming, since we dont have any other tags to go off, that your file would look something such as this from beginning to end
<?xml version="1.0" encoding="utf-8" ?>
<RootElement>
<UserInformation>
<Name>AAA</Name>
<Age>23</Age>
<I1>
<Element1>A</Element1>
<Element2>B</Element2>
<Element3>C</Element3>
<\I1>
<I2>
<Element1>AA</Element1>
<Element2>BB</Element2>
<Element3>CC</Element3>
</I2>
</UserInformation>
</RootElement>
and then to call it
System.IO.StreamReader sr = new System.IO.StreamReader("test.xml");
String xmlText = sr.ReadToEnd();
sr.Close();
List<UserInfo> finalList = readXMLDoc(xmlText);
if(finalList != null)
{
//do something
}
private List<UserInfo> readXMLDoc(String fileText)
{
//create a list of Strings to hold our user information
List<UserInfo> userList = new List<UserInfo>();
try
{
//create a XmlDocument Object
XmlDocument xDoc = new XmlDocument();
//load the text of the file into the XmlDocument Object
xDoc.LoadXml(fileText);
//Create a XmlNode object to hold the root node of the XmlDocument
XmlNode rootNode = null;
//get the root element in the xml document
for (int i = 0; i < xDoc.ChildNodes.Count; i++)
{
//check to see if we hit the root element
if (xDoc.ChildNodes[i].Name == "RootElement")
{
//assign the root node
rootNode = xDoc.ChildNodes[i];
break;
}
}
//Loop through each of the child nodes of the root node
for (int j = 0; j < rootNode.ChildNodes.Count; j++)
{
//check for the UserInformation tag
if (rootNode.ChildNodes[j].Name == "UserInformation")
{
//assign the item node
XmlNode userNode = rootNode.ChildNodes[j];
//create userInfo object to hold results
UserInfo userInfo = new UserInfo();
//loop through each if the user tag's elements
foreach (XmlNode subNode in userNode.ChildNodes)
{
//check for the name tag
if (subNode.Name == "Name")
{
userInfo._name = subNode.InnerText;
}
//check for the age tag
if (subNode.Name == "Age")
{
userInfo._age = subNode.InnerText;
}
String tagToLookFor = "CallTheMethodThatReturnsTheCorrectTag";
//check for the tag
if (subNode.Name == tagToLookFor)
{
foreach (XmlNode elementNode in subNode.ChildNodes)
{
//check for the element1 tag
if (elementNode.Name == "Element1")
{
userInfo._element1 = elementNode.InnerText;
}
//check for the element2 tag
if (elementNode.Name == "Element2")
{
userInfo._element2 = elementNode.InnerText;
}
//check for the element3 tag
if (elementNode.Name == "Element3")
{
userInfo._element3 = elementNode.InnerText;
}
}
}
}
//add the userInfo to the list
userList.Add(userInfo);
}
}
}
catch (Exception e)
{
System.Windows.Forms.MessageBox.Show(e.Message);
return null;
}
//return the list
return userList;
}
//struct to hold information
struct UserInfo
{
public String _name;
public String _age;
public String _element1;
public String _element2;
public String _element3;
}