converting large xml to csv - c#

I am wanting to convert a large amount of xml data near 600mb where i wont no its headers or root nodes I did try the following code but its complaining it does not no its root nodes. What is the most effiecent way of processiing a large file.
using (FileStream fs = new FileStream(#"C:\Projects\csvExport\csvExport\csvExport\bin\VLFLBNM7.xml", FileMode.Open))
{
XmlRootAttribute xRoot = new XmlRootAttribute();
xRoot.ElementName = "ReportDetails";
// xRoot.Namespace = "http://www.cpandl.com";
xRoot.IsNullable = true;
XmlSerializer serializer = new XmlSerializer(typeof(Sequence[]),xRoot);
var data = (Sequence[])serializer.Deserialize(fs);
List<string> list = new List<string>();
foreach (var item in data)
{
List<string> ss = new List<string>();
foreach (var point in item.SourcePath) ss.Add(point.X + "," + point.Y);
list.Add(string.Join(",", ss));
}
File.WriteAllLines(#"C:\Projects\csvExport\csvExport\csvExport\bin\csvFile.csv", list);
}

This code works fine if your XML is having root node with Name "ReportDetails"
Sample XML file for this code to work should be like
<?xml version="1.0"?>
<ReportDetails>
<Sequence>
<SourcePath>
<X>samply x</X>
<Y>xample y</Y>
</SourcePath>
</Sequence>
</ReportDetails>
Your XML is missing "ReportDetails" which you have specified in code
xRoot.ElementName = "ReportDetails";

Related

add new lines when generating xml from object

this is the code
XmlDocument xml = new XmlDocument();
XmlElement root = xml.CreateElement("customers");
xml.AppendChild(root);
foreach (var cust in customerlist)
{
XmlElement child = xml.CreateElement("customer");
child.SetAttribute("CustomerId", cust.CustomerId.ToString());
child.SetAttribute("CustomerName", cust.CustomerName);
child.SetAttribute("PhoneNumber", cust.PhoneNumber);
child.SetAttribute("Email", cust.Email);
root.AppendChild(child);
}
string s = xml.OuterXml;
I want my string to have next lines added to it instead of a single xml document
My string is coming as continuous
< x >xxxxx< /x > < x >xxxxx< /x >
You can use the XmlTextWriter class to format the XML as a string like this:
StringWriter string_writer = new StringWriter();
XmlTextWriter xml_text_writer = new XmlTextWriter(string_writer);
xml_text_writer.Formatting = Formatting.Indented;
xml.WriteTo(xml_text_writer); // xml is your XmlDocument
string formattedXml = string_writer.ToString();

xdocument re writing xml document [duplicate]

I have a method which load XML to a XDocument and modify its elements then save.
But when I reload it. I got this error :
Unexpected XML declaration. The XML declaration must be the first node in the document, and no white space characters are allowed to appear before it.
I checking the XML and see that the XDocument didn't save the changed but create a duplicate and save.
It save the old one and the new one like this example xml :
<?xml version="1.0" encoding="UTF-8"?>
<Ungdungs>
<Ungdung>
<Name>HERE City Lens</Name>
<Id>b0a0ac22-cf9e-45ba-8120-815450e2fd71</Id>
<Path>/Icon/herecitylens.png</Path>
<Version>Unknown</Version>
<Category>HERE</Category>
<Date>Uknown</Date>
</Ungdung>
<?xml version="1.0" encoding="UTF-8"?>
<Ungdungs>
<Ungdung>
<Name>HERE City Lens</Name>
<Id>b0a0ac22-cf9e-45ba-8120-815450e2fd71</Id>
<Path>/Icon/herecitylens.png</Path>
<Version>1.0.0.0</Version>
<Category>HERE</Category>
<Date>Uknown</Date>
</Ungdung>
Here the code I used to modify and save XML :
using (Stream stream = storage.OpenFile("APPSDATA.xml", FileMode.Open, FileAccess.ReadWrite))
{
//var xdoc = XDocument.Load("APPSDATA.xml");
var xdoc = XDocument.Load(stream, LoadOptions.None);
var listapp = from c in xdoc.Descendants("Ungdung") select c;
foreach (XElement app in listapp)
{
var xElement = app.Element("Name");
if (xElement != null)
progressIndicator.Text = "Checking " + xElement.Value + "...";
var element = app.Element("Id");
if (element != null)
{
var appId = element.Value;
var appVersion = await GetAppsVersion(appId);
app.Element("Version").Value = appVersion.ToString();
}
}
xdoc.Save(stream);
}
How can I solve this problem ?
Looks like you're appending modified document at the end of current file content. That's why you can't parse it later again.
I would split read and write parts into different using statements:
XDocument xdoc;
using (Stream stream = storage.OpenFile("APPSDATA.xml", FileMode.Open, FileAccess.Read))
{
xdoc = XDocument.Load(stream, LoadOptions.None);
}
var listapp = from c in xdoc.Descendants("Ungdung") select c;
foreach (XElement app in listapp)
{
var xElement = app.Element("Name");
if (xElement != null)
progressIndicator.Text = "Checking " + xElement.Value + "...";
var element = app.Element("Id");
if (element != null)
{
var appId = element.Value;
var appVersion = await GetAppsVersion(appId);
app.Element("Version").Value = appVersion.ToString();
}
}
using (Stream stream = storage.OpenFile("APPSDATA.xml", FileMode.Truncate, FileAccess.Write))
{
xdoc.Save(stream);
}
Setting FileMode.Truncate on second using statement will clear previous file content, what should fix your problem.

How to read xml file c#

<CPT xmlns="http://www.example.org/genericClientProfile" xmlns:ns2="http://www.blahblah.com" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.example.org/genericClientProfile genericClientProfile.xsd">
<header>
<serviceId>CPT-UK</serviceId>
<versionId>1.0</versionId>
<brandCode>CUK</brandCode>
<creationTime>2013-09-26T13:55:32.31+02:00</creationTime>
</header>
</CPT>
I need to be able to read the elements in the header tag. I'm struggling to read the values for some reason, which i'm not sure of. What i've tried:
public ActionResult readxmldata()
{
using (var db = new CPTEntities())
{
var file = System.IO.Directory.GetFiles("C:\\Workspace\\CPTStaging","*.xml");
foreach (var xmldoc in file)
{
XmlDocument docpath = new XmlDocument();
docpath.Load(xmldoc);
CPTPROFILE doc = new CPTPROFILE();
db.SaveChanges();
H_HEADER header = new H_HEADER();
header.SERVICEID = docpath.SelectSingleNode("//CPT/header/#serviceId").Value;
header.VERSIONID = Convert.ToDecimal(docpath.SelectSingleNode("//CPT/header/#versionId").Value);
header.CREATIONTIME = Convert.ToDateTime(docpath.SelectSingleNode("//CPT/header/#creationTime").Value);
header.BRANDCODE = docpath.SelectSingleNode("//CPT/header/#brandCode").Value;
db.CPTPROFILEs.AddObject(doc);
db.SaveChanges();
}
}
Your XML uses namespaces. xmlns attribute declares default namespace. You should use it for each element of the XML.
XNamespace ns = "http://www.example.org/genericClientProfile";
XDocument doc = XDocument.Load(xmldoc);
XElement header = doc.Root.Element(ns + "header");
For comparison, here is one way to do it with Linq-to-XML:
XDocument doc = XDocument.Load(xmlFileName);
XNamespace ns = "http://www.example.org/genericClientProfile";
var header = doc.Descendants(ns+"header").Single();
H_HEADER header = new H_HEADER();
header.SERVICEID = (string) header.Element(ns + "serviceId");
header.VERSIONID = (double) header.Element(ns + "versionId");
header.BRANDCODE = (string) header.Element(ns + "brandCode");
header.CREATIONTIME = (DateTime) header.Element(ns + "creationTime");

Returning elements of an XML file as a string

I'm attempting to display XML content tags from a word document that the user uploads, but I'm not sure how to explicitly pull out the data, and then display it as a string.
I think the below SHOULD find the correct descendent (or element, either should work right?) and then allow me to create a string out of it and display it, but I can't get the file to recognise xdoc). What I'm trying to return is located as "w.tag" in the bottom section of code.
Any ideas?
WordprocessingDocument _TempDoc = WordprocessingDocument.Open(Server.MapPath("~/") + filename, true);
//query to find particular descendants
var lv1s = from lv1 in xdoc.Descendants("table")
select new
{
Header = lv1.Attribute("name").Value,
Children = lv1.Descendants("tag")
};
//Loop through results
StringBuilder result = new StringBuilder();
foreach (var lv1 in lv1s)
{
result.AppendLine(lv1.Header);
foreach (var lv2 in lv1.Children)
result.AppendLine(" " + lv2.Attribute("name").Value);
}
//the label should contain the content controls of the document, using the class, XMLfromDocument
labelContentControls.Text = fileUpload_Displayx(XMLfromDocument.GetContentControls(_TempDoc));
public static XDocument GetXDocument(this OpenXmlPart part)
{
XDocument xdoc = part.Annotation<XDocument>();
if (xdoc != null)
return xdoc;
using (Stream str = part.GetStream())
using (StreamReader streamReader = new StreamReader(str))
using (XmlReader xr = XmlReader.Create(streamReader))
xdoc = XDocument.Load(xr);
part.AddAnnotation(xdoc);
return xdoc;
}
//following method gets the structure of the content controls / XML in the document
public static XElement GetContentControls( WordprocessingDocument document)
{
XElement contentControls = new XElement("ContentControls",
document
.MainDocumentPart
.GetXDocument()
.Root
.Element(W.body)
.Elements(W.sdt)
.Select(tableContentControl =>
new XElement("Table",
new XAttribute("Name", (string)tableContentControl
.Element(W.sdtPr).Element(W.tag).Attribute(
W.val)),
tableContentControl
.Descendants(W.sdt)
.Select(fieldContentControl =>
new XElement("Field",
new XAttribute("Name",
(string)fieldContentControl
.Element(W.sdtPr)
.Element(W.tag)
.Attribute(W.val)
)
)
)
)
)
);
// you cannot access the inner XML of the elemnt directly, you must concatenate to the child elements.
// string contentTitle = string.Concat(W.sdtPr);
//return W.tag;
return contentControls;

XmlException was unhandled on C# Windows Phone 7

I'm attempting to open an XML file which is copied as content to the IsolatedStorage when I compile my app and am having some issues with it;
Data at the root level is invalid. Line 1, position 411.
I'm not entirely sure what this means and a trip to my local search engine only expanded my confusion, could anybody please tell me if I'm doing anything drastically wrong or if my data structure is bad please?
Here's my function that loads the data from the file and parses it into variables:
private void ReadData()
{
using (var store = IsolatedStorageFile.GetUserStoreForApplication())
{
if (store.FileExists("appdata.xml"))
{
IsolatedStorageFileStream fileStream = new IsolatedStorageFileStream("appdata.xml", FileMode.Open, store);
XDocument document;
using (XmlReader reader = XmlReader.Create(fileStream))
{
if (reader != null)
{
document = XDocument.Load(reader); // <-- Error occurs here
ListBox listBox = new ListBox();
var data = from query in document.Descendants("myData")
select new DataHolder
{
CashData = (string)query.Element("CashData"),
LandGoData = (string)query.Element("LandGoData"),
FreeParkingData = (string)query.Element("FreeParkData"),
CircuitData = (string)query.Element("FullCircuitData"),
AuctionData = (string)query.Element("AuctionData")
};
listBox.ItemsSource = data;
StartCashRule.Text = (string)listBox.FindName("myData");
}
}
fileStream.Close();
}
}
}
And here's my xml document:
<?xml version="1.0" encoding="utf-8" ?>
<RootPath>
<CashData>Value1</CashData>
<LandGoData>Value2</LandGoData>
<FreeParkData>Value3</FreeParkData>
<FullCircuitData>Value4</FullCircuitData>
<AuctionData>Value5</AuctionData>
</RootPath>

Categories