I have the following XML structure
<T>
<F>
<H>
<H1>some value</H1>
<H2>some value</H2>
<H3>some value</H3>
</H>
<O>
<P>some value</P>
<TI>some value</TI>
<TI>some value</TI>
</O>
<R>
<PTY>some value</PTY>
<PTY>some value</PTY>
<PTY>some value</PTY>
</R>
</F>
<T>
I need to parse this xml in C# and get the values out of them to be further exported to a CSV file.
My query is how do you go about creating an entity for this XML
You can play with XmlSerializer and its related attributes.
As long as the XML is not too complex, there's not much work to do.
To read the XML:
var serializer = new XmlSerializer(typeof(SerializableObject));
SerializableObject deserialized;
using (var stream = new FileStream(#"C:\test.xml", FileMode.Open))
{
deserialized = (SerializableObject)serializer.Deserialize(stream);
}
The SerializableObject will look like this:
[Serializable]
[XmlRootAttribute("T")]
public class SerializableObject
{
...
}
BONUS for lazy programmers: You can just use Xsd.exe to brute force create an object from an XML file. Then tweak the results to your needs.
You can use LinqToXml to parse xml. StringBuilder will be helpful to produce CSV.
I think these How Tos will be useful. They describe all you needed to parse this xml.
add using System.Xml.Linq;
then you can do something similar to this:
XDocument xml = XDocument.Load(#"....\People.xml");
var query = from p in xml.Elements("people").Elements("person")
where (int)p.Element("id") == 1
select p;
Related
Below is an example of the xml file that I need to pull data via C#. This is my first experience with reading xml files and a beginner with xml. Anyone have an example of how I would find/load the fieldorder values for Export_B?
<?xml version="1.0" encoding="utf-8"?>
<Config>
<OutFolderCSV>c:\Output\2012\upload_Files</OutFolderCSV>
<OutFolderImage>c:\Output\2012\NM_Scorecard_Images</OutFolderImage>
<PathOutLogFile>c:\Output\2012\Log\Matches.log</PathOutLogFile>
<FieldSeparator>,</FieldSeparator>
<ExportFile>
<Name>Export_A</Name>
<FieldOrder>matchID</FieldOrder>
<FieldOrder>contactID</FieldOrder>
<FieldOrder>stageID13</FieldOrder>
<FieldOrder>stringScore1a</FieldOrder>
<FieldOrder>xScore1a</FieldOrder>
<FieldOrder>stageID14</FieldOrder>
<FieldOrder>stringScore1b</FieldOrder>
<FieldOrder>xScore1b</FieldOrder>
<FieldOrder>stageID15</FieldOrder>
<FieldOrder>stringScore1c</FieldOrder>
<FieldOrder>xScore1c</FieldOrder>
</ExportFile>
<ExportFile>
<Name>Export_B</Name>
<FieldOrder>matchID</FieldOrder>
<FieldOrder>contactID</FieldOrder>
<FieldOrder>stageID16</FieldOrder>
<FieldOrder>stringScore1a</FieldOrder>
<FieldOrder>xScore1a</FieldOrder>
<FieldOrder>stageID17</FieldOrder>
<FieldOrder>stringScore1b</FieldOrder>
<FieldOrder>xScore1b</FieldOrder>
<FieldOrder>stageID18</FieldOrder>
<FieldOrder>stringScore1c</FieldOrder>
<FieldOrder>xScore</FieldOrder>
</ExportFile>
</Config>
Using LINQ to XML:
var doc = XDocument.Load(#"c:\path\to\file.xml");
var fieldOrders =
from exportFile in doc.Descendants("ExportFile")
where (string)exportFile.Element("Name") == "Export_B"
from fieldOrder in exportFile.Elements("FieldOrder")
select (string)fieldOrder;
I have written an article
http://www.codeproject.com/Articles/33769/Basics-of-LINQ-Lamda-Expressions
on XML using XDocument object.
You can parse the XML easily using
XDocument.Load(filepath)
Please read the section XLinq to parse the objects.
edit :
You can change value of Export_B using the code :
var document = XDocument.Load(filepath)
var exportFiles = document.Descandants("ExportFile");
List<XElement> list = new List<XElement>();
foreach(var element in exportFiles)
{
list.Add(element);
// Now you can do element.Element("Name") to get the name. Put a breakpoint on this, you can get the reference of all underlying objects.
}
I have a project where i read XML that was exported from another system. The XML looks like this:
<?xml version="1.0" encoding="ISO-8859-1"?><xmlimexport>
<companydata/>
<articles/>
<customers/>
<suppliers/>
<orders>
<order>
<atOrder>
<OOI_HEAD_DOCUMENT_NUMBER>12345</OOI_HEAD_DOCUMENT_NUMBER>
**... more rows ...**
</atOrder>
<rows>
<row><OOI_ROW_ARTICLE_NUMBER>12345</OOI_ROW_ARTICLE_NUMBER><OOI_ROW_ARTICLE_TEXT>SuperDuperArticleName</OOI_ROW_TEXT>**... more data...**</row>
</rows>
</order>
</orders>
<bests/>
<invoices/>
<supplierinvoices/>
<pricelists/>
<parcels/>
</xmlimexport>
So what i do is load the path to the XML file then:
XmlDocument doc = new XmlDocument();
// Load xml file
doc.Load(xmlFile);
// Read order data
XmlNodeList orderList = doc.GetElementsByTagName("order");
foreach (XmlElement order in orderList)
{
try
{
// Read atOrder data (single node)
XmlNode atOrder = order.SelectSingleNode("atOrder");
// Read article data (one or many nodes)
XmlNodeList articles = order.GetElementsByTagName("row");
// Create a order
Order customerOrder = new Order();
Then read data with:
customerOrder.CUS_ID = Convert.ToInt32(atOrder.SelectSingleNode("OOI_HEAD_DOCUMENT_NUMBER").InnerText);
But since it can be both Strings, Booleans, Datetime, Date and INT in those fields i find myself having to use Convert. very much, is this the proper way to do this or should i use a different approach?
you can also generate an XSD and based on an XSD a class to deserialize the XML (short tutorial here)
you can read the XML directly into a DataSet (.ReadXML)
Have a look at LinqToXml I think that might help:
http://msdn.microsoft.com/en-us/library/bb387098.aspx
I'm wanting to offer a way to save work in an application. It is a sort of project functionality. The projects need to be serialized to XML. I've got DataContractSerializer generating the XML, but I need to be able to select a location in an XML file for it to serialize to instead of writing a complete file for itself since there will be multiple projects and I will need to be able to de/serialize these projects individually from a Projects XML file.
What I would like for XML:
<Projects>
<Project>
<ID>...</ID>
<Name>...</Name>
<LastEdited>...</LastEdited>
...
</Project>
<Project>...</Project>
<Project>...</Project>
</Projects>
I would then serialize and deserialize based on the Project ID using Linq to XML to navigate the doc.
Any suggestions? Much thanks in advance.
Found a way to do this. It has its hitches but once you get everything included it works pretty smoothly.
To generate the XML:
DataContractSerializer ser = new DataContractSerializer(typeof(Project));
StringBuilder objectSB = new StringBuilder();
XmlWriter objectWriter = XmlWriter.Create(objectSB);
//I'm using this method in the object I want to serialize (hence the 'this')
ser.WriteObject(objectWriter, this);
objectWriter.Flush();
Then you can turn the StringBuilder into an XElement for Linq to XML with:
Xelement xProject = Xelement.Parse(objectSB.ToString());
For deserialization:
DataContractSerializer ser = new DataContractSerializer(typeof(Project));
Project project = (CoilProject)ser.ReadObject(xProject.CreateReader());
There are a few times when I edit the XML using Linq to XML without deserializing and reserializing. Due to the namespaces that the DataContractSerializer adds, this becomes necessary:
//place this somewhere in your project's namespace
public static class MyExtensions
{
public static IEnumerable<XElement> ElementsAnyNS(this XElement source, string localName)
{
return source.Elements().Where(e => e.Name.LocalName == localName);
}
public static XElement ElementAnyNS(this XElement source, string localName)
{
return source.Elements().Where(e => e.Name.LocalName == localName).Select(e => e).Single();
}
}
Those extension methods allow you to select one or many elements from the serialized object without worrying about namespace. I adapted these extension methods from Pavel Minaev's answer on Ignore namespaces in LINQ to XML.
I'm using
List<EFacebook> facebooks = BFacebook.ReadFacebookFriends(user.EProviders
.Where(i => i.ProviderType == EProvider.EnumProviderType.Facebook)
.First().Token);
StringBuilder xml = new StringBuilder();
foreach (EFacebook i in facebooks)
{
xml.AppendFormat("<id>{0}</id>", i.id);
}
Can anybody suggest a better code to serialize each i.id into an XML string?
Edit:
The facebook object has close to 20 properties. If I XmlSerializer all 20 properties are serialized into the XML. I just need the id column.
You might want to take a look at XML Serialization already built into the .NET Framework.
You can use code similar to the following to serialize the object:
MySerializableClass myObject = new MySerializableClass();
// Insert code to set properties and fields of the object.
XmlSerializer mySerializer = new
XmlSerializer(typeof(MySerializableClass));
// To write to a file, create a StreamWriter object.
StreamWriter myWriter = new StreamWriter("myFileName.xml");
mySerializer.Serialize(myWriter, myObject);
myWriter.Close();
See: How to serialize
You can flag properties to be ignored using the XmlIgnore attribute as shown below:
public class Group
{
// The XmlSerializer ignores this field.
[XmlIgnore]
public string Comment;
// The XmlSerializer serializes this field.
public string GroupName;
}
See XMLIgnore
XmlSerializer xs = new XmlSerializer(typeof(int[]));
xs.Serialize(stream,facebooks.Select(x=>x.id).ToArray())
I would use Linq To Xml to create the Xml Tree structure
See for an example: Linq To Xml
If you're just saving the id values into an xml string then I wouldn't say that you're 'serializing' the objects.
Whenever working with XML it's much nicer to use an XML library than to wrangle with text. One obvious reason is that it guarantees your XML will be well-formed.
I'd do something like this:
List<EFacebook> facebooks = GetFriends();
var facebookIds = facebooks.Select(f => new XElement("id", f.id));
var facebookXml = new XElement("facebookIds", facebookIds);
Which will give you XML like
<facebookIds>
<id>1</id>
<id>2</id>
</facebookIds>
I currently load an XML file into a list objects using code like this
XDocument xmlDoc = XDocument.Load(path);
List<ImportDefinition> importDefinitions = xmlDoc.Descendants("Root").Select(xElem => (ImportDefinition)xElem).ToList();
return importDefinitions;
This list of objects contains nested objects and each one has an operator for parsing the XML into the correct form like this
public static explicit operator Rules(XElement xElem)
{
try
{
return new Rules()
{
FileNameRegEx = (string)xElem.Element("FileNameRegEx"),
FileExtension = (string)xElem.Element("FileExtension")
};
}
catch (Exception ex)
{
return null;
}
This works fine for loading the XML. I now want to save this list of objects back to XML after some edits have been made.
I was hoping something like this would work
XElement xml = new XElement("Root",
from p in ObjectList
select new XElement("File",RootObject
));
}
xml.Save("C:\\temp\\newimport.xml");
However this just seems to output this
<?xml version="1.0" encoding="utf-8"?>
<Root>
<File>MyNamespace.RootObject</File>
<File>MyNamespace.RootObject</File>
</Root>
It looks like its not using the custom operators it uses when loading the files to work out the format to save in. Whats the best way to save this data back to XML to the same format it was in when I read it?
Well for one thing you've only shown us the operator for parsing from an XElement... but even so, you're obviously explicitly calling that in your LINQ expression. If you want the equivalent when building XML, you'll need to be explicit there too:
XElement xml = new XElement("Root",
from p in ObjectList
select new XElement("File", (XElement) p));
Personally I'd use methods instead of operators - ToXElement() and FromXElement() - I think it's clearer that way. ToXElement would be an instance method; FromXElement would be a static method. This is a pattern I've used many times, and it's always worked fine.