Append a record to Xml ASP.Net MVC5 - c#

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

Related

XDocument get Element value with Namespace (RSS Feed)

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

Deserialize Xml file with root element, elemnts and child element c#

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")
};

how to parse XML using XmlReader along with their closing tags?

Consider the following XML which I have to parse.
<root>
<item>
<itemId>001</itemId>
<itemName>test 1</itemName>
<description/>
</item>
</root>
I have to parse each of its tag and store it into a table as follows:
TAG_NAME TAG_VALUE IsContainer
------------ -------------- -----------
root null true
item null true
itemId 001 false
itemName test 1 false
description null false
/item null true
/root null true
Now to get this done, I am using XmlReader as this allows us to parse each & every node.
I am doing it as follows:
I created the following class to contain each tag's data
public class XmlTag
{
public string XML_TAG { get; set; }
public string XML_VALUE { get; set; }
public bool IsContainer { get; set; }
}
I am trying to get the list of tags(including closing ones) as follows:
private static List<XmlTag> ParseXml(string path)
{
var tags = new List<XmlTag>();
using (var reader = XmlReader.Create(path))
{
while (reader.Read())
{
var tag = new XmlTag();
bool shouldAdd = false;
switch (reader.NodeType)
{
case XmlNodeType.Element:
shouldAdd = true;
tag.XML_TAG = reader.Name;
//How do I get the VALUE of current reader?
//How do I determine if the current node contains children nodes to set IsContainer property of XmlTag object?
break;
case XmlNodeType.EndElement:
shouldAdd = true;
tag.XML_TAG = string.Format("/{0}", reader.Name);
tag.XML_VALUE = null;
//How do I determine if the current closing node belongs to a node which had children.. like ROOT or ITEM in above example?
break;
}
if(shouldAdd)
tags.Add(tag);
}
}
return tags;
}
but I am having difficulty determining the following:
How to determine if current ELEMENT contains children XML nodes? To set IsContainer property.
How to get the value of current node value if it is of type XmlNodeType.Element
Edit:
I have tried to use LINQ to XML as follows:
var xdoc = XDocument.Load(#"SampleItem.xml");
var tags = (from t in xdoc.Descendants()
select new XmlTag
{
XML_TAG = t.Name.ToString(),
ML_VALUE = t.HasElements ? null : t.Value,
IsContainer = t.HasElements
}).ToList();
This gives me the XML tags and their values but this does not give me ALL the tags including the closing ones. That's why I decided to try XmlReader. But If I have missed anything in LINQ to XML example, please correct me.
First of all, as noted by Jon Skeet in the comments you should probably consider using other tools, like XmlDocument possibly with LINQ to XML (EDIT: an example with XmlDocument follows).
Having said that, here is the simplest solution for what you have currently (note that it's not the cleanest possible code, and it doesn't have much validation):
private static List<XmlTag> ParseElement(XmlReader reader, XmlTag element)
{
var result = new List<XmlTag>() { element };
while (reader.Read())
{
switch (reader.NodeType)
{
case XmlNodeType.Element:
element.IsContainer = true;
var newTag = new XmlTag() { XML_TAG = reader.Name };
if (reader.IsEmptyElement)
{
result.Add(newTag);
}
else
{
result.AddRange(ParseElement(reader, newTag));
}
break;
case XmlNodeType.Text:
element.XML_VALUE = reader.Value;
break;
case XmlNodeType.EndElement:
if (reader.Name == element.XML_TAG)
{
result.Add(new XmlTag()
{
XML_TAG = string.Format("/{0}", reader.Name),
IsContainer = element.IsContainer
});
}
return result;
}
}
return result;
}
private static List<XmlTag> ParseXml(string path)
{
var result = new List<XmlTag>();
using (var reader = XmlReader.Create(path))
{
while (reader.Read())
{
if (reader.NodeType == XmlNodeType.Element)
{
result.AddRange(ParseElement(
reader,
new XmlTag() { XML_TAG = reader.Name }));
}
else if (reader.NodeType == XmlNodeType.EndElement)
{
result.Add(new XmlTag()
{
XML_TAG = string.Format("/{0}",current.Name)
});
}
}
}
return result;
}
An example using XmlDocument. This will give slightly different result for self-enclosing tags (<description/> in your case). You can change this behaviour easily, depending on what you want.
private static IEnumerable<XmlTag> ProcessElement(XElement current)
{
if (current.HasElements)
{
yield return new XmlTag()
{
XML_TAG = current.Name.ToString(),
IsContainer = true
};
foreach (var tag in current
.Elements()
.SelectMany(e => ProcessElement(e)))
{
yield return tag;
}
yield return new XmlTag()
{
XML_TAG = string.Format("/{0}", current.Name.ToString()),
IsContainer = true
};
}
else
{
yield return new XmlTag()
{
XML_TAG = current.Name.ToString(),
XML_VALUE = current.Value
};
yield return new XmlTag()
{
XML_TAG = string.Format("/{0}",current.Name.ToString())
};
}
}
And using it:
var xdoc = XDocument.Load(#"test.xml");
var tags = ProcessElement(xdoc.Root).ToList();

How to get all different element tags from root tag using XPath

I have a following xml file
<root>
<element1>
<header>header1</header>
<tag1>tag1</tag1>
<response>
<status>success</status>
<Data>
<id>d1</id>
<test>2</test>
</Data>
<Beta>
<betaid>sdsd</betaid>
<code>123</code>
<code>ddd</code>
</Beta>
</response>
</element1>
</root>
My Question: How to get the first child elements under "Response" tag? i.e staus, data and beta.
Using XPath in C#. Thank you
The .net code i have is here but it does not work.
XPathDocument doc= new XPathDocument(XmlReaderdata);
XPathNavigator mes, Nav = doc.CreateNavigator();
foreach(XPathNavigator node in (XPathNodeIterator)Nav.Evaluate("//response/*)
{
node.Name;
}
An XPath query like this should work:
//response/*
For example:
var xml = #"<root> ... </root>";
using (StringReader stream = new StringReader(xml))
{
XPathDocument doc= new XPathDocument(stream);
XPathNavigator nav = doc.CreateNavigator();
XPathNodeIterator itor = (XPathNodeIterator)nav.Evaluate("//response/*");
foreach(XPathNavigator node in itor)
{
Console.WriteLine(node.Name);
}
}
Produces the output:
status
Data
Beta
You can use //response:
DTO:
public class Response
{
public Response()
{
Data = new List<KeyValuePair<string, string>>();
Beta = new List<KeyValuePair<string, string>>();
}
public string Status { get; set; }
public List<KeyValuePair<string, string>> Data { get; set; }
public List<KeyValuePair<string, string>> Beta { get; set; }
}
Code:
var document = XDocument.Parse(data);
var element = document.XPathSelectElement("//response");
var response = new Response();
response.Status = element.Element("status").Value;
foreach(var dataElement in element.Element("Data").Elements())
{
response.Data.Add(new KeyValuePair<string, string>(dataElement.Name.LocalName, dataElement.Value));
}
foreach(var betaElement in element.Element("Beta").Elements())
{
response.Beta.Add(new KeyValuePair<string, string>(betaElement.Name.LocalName, betaElement.Value));
}
Edit: Partial loading
public Response ParseResponse(XPathDocument document)
{
var navigator = document.CreateNavigator();
var iterator = navigator.Select("//response");
iterator.MoveNext();
var responseDoc = XDocument.Parse(iterator.Current.OuterXml);
var element = responseDoc.Element("response");
var response = new Response();
response.Status = element.Element("status").Value;
foreach(var dataElement in element.Element("Data").Elements())
{
response.Data.Add(new KeyValuePair<string, string>(dataElement.Name.LocalName, dataElement.Value));
}
foreach(var betaElement in element.Element("Beta").Elements())
{
response.Beta.Add(new KeyValuePair<string, string>(betaElement.Name.LocalName, betaElement.Value));
}
return response;
}
Response response;
using(var reader = XmlReader.Create(stream))
{
var doc = new XPathDocument(reader);
response = ParseResponse(doc);
}
Are you trying to get the names of the child nodes?
i hope this will help
string xmlstring = "<root><element><response><status>Success</status><Data><id>1</id></Data></response></element></root>";
XmlDocument doc = new XmlDocument();
doc.LoadXml(xmlstring);
XmlNode node = doc.SelectSingleNode("/root/element/response");
XmlNodeList children = node.ChildNodes;
foreach(XmlNode i in children )
Console.WriteLine(i.Name);

Serializing a list of objects to XDocument

I'm trying to use the following code to serialize a list of objects into XDocument, but I'm getting an error stating that "Non white space characters cannot be added to content
"
public XDocument GetEngagement(MyApplication application)
{
ProxyClient client = new ProxyClient();
List<Engagement> engs;
List<Engagement> allEngs = new List<Engagement>();
foreach (Applicant app in application.Applicants)
{
engs = new List<Engagement>();
engs = client.GetEngagements("myConnString", app.SSN.ToString());
allEngs.AddRange(engs);
}
DataContractSerializer ser = new DataContractSerializer(allEngs.GetType());
StringBuilder sb = new StringBuilder();
System.Xml.XmlWriterSettings xws = new System.Xml.XmlWriterSettings();
xws.OmitXmlDeclaration = true;
xws.Indent = true;
using (System.Xml.XmlWriter xw = System.Xml.XmlWriter.Create(sb, xws))
{
ser.WriteObject(xw, allEngs);
}
return new XDocument(sb.ToString());
}
What am I doing wrong? Is it the XDocument constructor that doesn't take a list of objects? how do solve this?
I would think that last line should be
return XDocument.Parse(sb.ToString());
And it might be an idea to cut out the serializer altogether, it should be easy to directly create an XDoc from the List<> . That gives you full control over the outcome.
Roughly:
var xDoc = new XDocument( new XElement("Engagements",
from eng in allEngs
select new XElement ("Engagement",
new XAttribute("Name", eng.Name),
new XElement("When", eng.When) )
));
The ctor of XDocument expects other objects like XElement and XAttribute. Have a look at the documentation. What you are looking for is XDocument.Parse(...).
The following should work too (not tested):
XDocument doc = new XDocument();
XmlWriter writer = doc.CreateNavigator().AppendChild();
Now you can write directly into the document without using a StringBuilder. Should be much faster.
I have done the job this way.
private void button2_Click(object sender, EventArgs e)
{
List<BrokerInfo> listOfBroker = new List<BrokerInfo>()
{
new BrokerInfo { Section = "TestSec1", Lineitem ="TestLi1" },
new BrokerInfo { Section = "TestSec2", Lineitem = "TestLi2" },
new BrokerInfo { Section = "TestSec3", Lineitem ="TestLi3" }
};
var xDoc = new XDocument(new XElement("Engagements",
new XElement("BrokerData",
from broker in listOfBroker
select new XElement("BrokerInfo",
new XElement("Section", broker.Section),
new XElement("When", broker.Lineitem))
)));
xDoc.Save("D:\\BrokerInfo.xml");
}
public class BrokerInfo
{
public string Section { get; set; }
public string Lineitem { get; set; }
}

Categories

Resources