I currently have a XML file format that goes something like this (whitespace and ellipses added for readability):
<root>
<Module> //Start with list of Modules
<ModuleParams>
</ModuleParams>
</Module>
...
<DetectLine> //Now a list of DetectLines
<DetectLineParams>
</DetectLineParams>
<Channels> //List of Channels embedded in each DetectLine
<Channel>
<ChannelParams>
</ChannelParams>
</Channel>
...
</Channels>
</DetectLine>
...
</root>
Classes structured as follows:
public class Module
{
public ModuleParams { get; set; }
}
public class DetectLine
{
public DetectLineParams { get; set; }
public List<Channel> Channels { get; set; }
}
public class Channel
{
public ChannelParams { get; set; }
}
The list of Modules and DetectLines are easy to parse with Linq to XML. However, parsing the Channel list for each DetectLine is not as apparent to me. Can this even be done with Linq to XML? I would prefer not having to restructure things to work with a XMLSerializer.
Initial Code (openXML is a OpenFileDialog. Already checked for good filename):
List<Module> myModules;
List<DetectLine> myDetectLines;
XDocument config = XDocument.Load(openXML.FileName);
myModules =
(from myElements in config.Descendants("Module")
select new Module()
{
ModuleParam1 = (string)myElements.Element("ModuleParam1"),
ModuleParam2 = (string)myElements.Element("ModuleParam2"),
...
}).ToList<Module>();
myDetectLines =
(from myElements in config.Descendants("DetectLine")
select new DetectLine()
{
DetectLineParam1 = (string)myElements.Element("ModuleParam1"),
DetectLineParam2 = (string)myElements.Element("ModuleParam2"),
...
// ?? Add Channels list here somehow...
}).ToList<DetectLine>();
With
XElement detectLine = XElement.Parse(#"<DetectLine>
<DetectLineParams>
</DetectLineParams>
<Channels>
<Channel>
<ChannelParams>
</ChannelParams>
</Channel>
...
</Channels>
</DetectLine>
");
you can do e.g.
DetectLine dl1 = new DetectLine() {
DetectLineParams = ...,
Channels = (from channel in detectLine.Element("Channels").Element("Channel")
select new Channel() {
ChannelParams = new ChannelParams() { ... = channel.Element("ChannelParams").Value }
}).ToList();
We really need to see more of the concrete C# class code to spell out how to set up the complete query.
[edit]
To fit in with the code you have now posted:
myDetectLines =
(from myElements in config.Descendants("DetectLine")
select new DetectLine()
{
DetectLineParam1 = (string)myElements.Element("ModuleParam1"),
DetectLineParam2 = (string)myElements.Element("ModuleParam2"),
...
Channels = (from channel in myElements.Element("Channels").Element("Channel")
select new Channel() {
ChannelParams = new ChannelParams() { ... = channel.Element("ChannelParams").Value }
}).ToList();
}).ToList<DetectLine>();
Related
Due to one of the interfaces we are writing for, we have to add a CDATA tag for a list of classes.
<modules>
<![CDATA[<module>
<title></title>
<code></code>
<level></level>
<year></year>
<summary></summary>
</module>
<module>
<title></title>
<code></code>
<level></level>
<year></year>
<summary></summary>
</module>]]>
</modules>
I'm unsure how to achieve this. I have found questions around individual strings, but not so much around an entire class.
Any suggestions would be helpful.
One of the ways to get the output you are expecting is to separate the creation of the module data and generating the CDATA part. For example:
To create the module data -
Create a class to hold module details as below -
public class Module
{
public string title { get; set; }
public string code { get; set; }
public string level { get; set; }
public string summary { get; set; }
}
Create a method to fetch these datails -
public static string CreateXMLString()
{
List<Module> modules = new List<Module>();
modules = new List<Module>() { new Module() { code = "test",
summary="Test3", title="Test", level = "tests1" },
new Module() { code = "test3",
summary="Test3", title="Test3", level = "tests3" } };
// Create XML to return the string in the format of
// <module code="test">
// < level > tests1 </ level >
// < summary > Test3 </ summary >
// < title > Test </ title >
//</ module >< module code = "test3" >
// < level > tests3 </ level >
// < summary > Test3 </ summary >
// < title > Test3 </ title >
// </ module >
var modulesXml =
from mod in modules
select new XElement("module",
new XAttribute("code", mod.code),
new XElement("level", mod.level),
new XElement("summary", mod.summary),
new XElement("title", mod.title)
);
return String.Concat(modulesXml);
}
To get the CDATA you can use the below steps -
Create a class Modulesand refer the documentation for usages of CreateCDataSection and for similar threads here for the details
[XmlType("")]
public class Modules
{
public Modules() { }
[XmlIgnore]
public string Message { get; set; }
[XmlElement("modules")]
public System.Xml.XmlCDataSection MyStringCDATA
{
get
{
return new System.Xml.XmlDocument().CreateCDataSection(Message);
}
set
{
Message = value.Value;
}
}
}
To test the output assign the string generated in step 2 during serialization you can refer the sample code below
static void Main(string[] args)
{
Modules mc = new Modules();
mc.Message = CreateXMLString();//Assign your data created in step 2
XmlSerializer serializer = new XmlSerializer(typeof(Modules));
XmlSerializerNamespaces ns = new XmlSerializerNamespaces();
ns.Add("", "");
StringWriter writer = new StringWriter();
//Remove unnecessary namespaces
serializer.Serialize(writer, mc,ns);
var test = XDocument.Parse(writer.ToString());
var data = test.Root.Elements();
Console.WriteLine(data.FirstOrDefault().Value);
}
Output -
<modules>
<![CDATA[<module>
<code>test</code>
<level>tests1</level>
<summary>Test3</summary>
<title>Test</title>
</module><module>
<code>test3</code>
<level>tests3</level>
<summary>Test3</summary>
<title>Test3</title>
</module>]]>
</modules>
In app a get a response from vk server with information about user's playlist in xml. App throw InvalidOperationexception here
var result = (PlayList)serializer.Deserialize(reader);
DTOs:
public class PlayList
{
[XmlElement("audio")]
public List<Song> Audio { get; set; }
public PlayList()
{
Audio = new List<Song>();
}
}
public class Song
{
[XmlElement(ElementName ="id")]
public int Id { get; set; }
...
[XmlElement(ElementName ="genre_id")]
public int Genre_id { get; set; }``
}
But my code work when I delete this 3 lines from xmlfile
<response>
<count>156</count>
...
<response>
and <items list="true"> -> <items>
What I must change in my code to make it work?
var serializer = new XmlSerializer(typeof(PlayList), new XmlRootAttribute("items"));
using (var stringReader = new StringReader(xml))
using (var reader = XmlReader.Create(stringReader))
{
var result = (PlayList)serializer.Deserialize(reader);
Console.WriteLine(result.Audio[1].Title);
}
And this is an example of xml.
How do I Deserialize this XML document?
var xml =
#"<?xml version="1.0" encoding="utf-8"?>
<response>
<count>156</count>
<items list="true">
<audio>
<id>456239034</id>
<owner_id>181216176</owner_id>
<artist>Oh Wonder</artist>
<title>Technicolour Beat</title>
<duration>179</duration>
<date>1465249779</date>
<url>http://cs613222.vk.me/u285269348/audios/4ae051b98797.mp3?extra=MS3dvwutPkFx7k8rQdV3Szuh7cSwLkRcS_KpPbO9DXviMFLNNgkDAmZFWdIueioL3dDgdPUc7rch0V81KgOHYaTTSampaRcljxrJcytJYImZssivVP7DigKdcxaLoALeUatAhuHk5gXQ7TY</url>
<lyrics_id>235824304</lyrics_id>
<genre_id>1001</genre_id>
</audio>
<audio>
<id>456239033</id>
<owner_id>181216176</owner_id>
<artist>Mikky Ekko</artist>
<title>We Must Be Killers (Волчонок / Teen Wolf / 2х08) </title>
<duration>195</duration>
<date>1465249755</date>
<url>http://cs521610.vk.me/u14558277/audios/c2daca7b2b6f.mp3?extra=z9VPdKf6v- n7zkIfZ_6ej-RZSjlIjAr_qYmVp4F-zI1Z3ZXgVtOUElovlOiSOgSuKbFC0e0ahac8XU-AxNtfEYYPe5gcejSotr84mHi0LQ2L-b0BPWP2cYn5Yy44YN4FLPNKq0Ow8vMKFn0</url>
<lyrics_id>26311225</lyrics_id>
</audio>
</items>
</response>";
You need to let it know its an array, I think using typeof(PlayList[]) instead of typeof(PlayList)
You can just skip unnecessary nodes with the XmlReader.
using (var reader = XmlReader.Create(stringReader))
{
reader.ReadToFollowing("items");
var result = (PlayList)serializer.Deserialize(reader);
I'm just trying to understand Linq and I am trying to do something that seems very simple, but I can't get it to output the way I would like. I have been stuck on this for days trying various different methods I just can't get it right.
So I have a class EarObs, it has members: eventID, icaoId, frm, sta, db.
I'm trying to build an XML document from a List. I want the XML document to look like so:
<EarObs EventId = "123456789">
<icao icaoID = "0001">
<frm frm = "01">
<sta sta = "00">
<db>87</db>
<hz>99</hz>
</sta>
<sta station = "01">
<db>79</db>
<hz>99</hz>
</sta>
</frm>
<frm frm = "02">
................
</frm>
</icao>
</EarObs>
And this would continue all the way down keeping the same order if there was more than one frame or more than one code etc.
So this is what I have been trying most recently but it still does not output they way I would like, Obs get repeated and I do not know where I am going wrong.
string eventGUID = "eventGUID";
List<EarObs> frameObsList = new List<EarObs>();
for (int frm = 2; frm > 0; frm--)
{
for (int sta = 5; sta > 0; sta--)
{
frameObsList.Add(new EarObs("KAPF", eventGUID, frm, sta, 85 + sta, 99 + sta));
cnt++;
}
}
String eventID = obsList.First().EventGUID;
List<EarObs> distinctApts =
obsList
.GroupBy(p => p.IcaoId)
.Select(g => g.First())
.ToList();
XElement xElement = new XElement("EarObs", new XAttribute("eventID", eventID),
from ea in distinctApts
orderby ea.IcaoId
select new XElement("icao", new XAttribute("code", ea.IcaoId),
from eb in obsList
where ea.IcaoId == eb.IcaoId
orderby eb.Frm
select new XElement("frm", new XAttribute("frm", eb.Frm),
from ec in obsList
where eb.Frm == ec.Frm
orderby ec.Sta
select new XElement("sta", new XAttribute("sta", ec.Sta),
new XElement("db", ec.Db),
new XElement("hz", ec.Hz)))));
Using this code I get an xml document that repeats the frame once for each station. This is not correct. I feel like this is easily done sequentially, but I'm trying to learn and this seems just so simple that I should be able to do it in Linq. I need each element in the List to only be represented in the XML document once. How do I go about this?
I would also like to expand it so that it can handle multiple eventId's as well, but that is not as important as getting the XML structure right. Any help would be much appreciated, I haven't been able to find too many example of creating an XML including the filtering of the elements using linq, most examples seem to have the List all ready structured before they create the XML.
Since you have a custom class, EarObs why not define Xml attributes to your object and serialize the object using the XmlSerlizer class? This way, you can continue use Linq on your objects, and also output your objects.
e.g. Below is a team, with players on it.
[XmlRoot("root")]
public class Team
{
private List<Player> players = new List<Player>();
[XmlElement("player")]
public List<Player> Players { get { return this.players; } set { this.players = value; } }
// serializer requires a parameterless constructor class
public Team() { }
}
public class Player
{
private List<int> verticalLeaps = new List<int>();
[XmlElement]
public string FirstName { get; set; }
[XmlElement]
public string LastName { get; set; }
[XmlElement]
public List<int> vertLeap { get { return this.verticalLeaps; } set { this.verticalLeaps = value; } }
// serializer requires a parameterless constructor class
public Player() { }
}
Once I create a team, with some players on it, I just have to do:
Team myTeamData = new Team();
// add some players on it.
XmlSerializer deserializer = new XmlSerializer(typeof(Team));
using (TextReader textReader = new StreamReader(#"C:\temp\temp.txt"))
{
myTeamData = (Team)deserializer.Deserialize(textReader);
textReader.Close();
}
The output will look like this:
<?xml version="1.0" encoding="utf-8"?>
<root xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<player>
<FirstName>dwight</FirstName>
<LastName>howard</LastName>
<vertLeap>1</vertLeap>
<vertLeap>2</vertLeap>
<vertLeap>3</vertLeap>
</player>
<player>
<FirstName>dwight</FirstName>
<LastName>howard</LastName>
<vertLeap>1</vertLeap>
</player>
</root>
The easiest way is to create a set of classes to handle the serialization like so;
public class sta
{
public int db { get; set; }
public int hz { get; set; }
[XmlAttribute()]
public string station { get; set; }
}
public class frm
{
[XmlAttribute("frm")]
public string frmID { get; set; }
[XmlElement("sta")]
public List<sta> stas { get; set; }
}
public class icao
{
[XmlAttribute]
public string icaoID { get; set; }
[XmlElement("frm")]
public List<frm> frms { get; set; }
}
public class EarObs
{
[XmlAttribute]
public string EventId { get; set; }
[XmlElement("icao")]
public List<icao> icaos { get; set; }
}
and you can use the xml serializer to serialize/deserialize. The following serializes to the structure identical to what you have;
XmlSerializer serializer = new XmlSerializer(typeof(EarObs));
EarObs obs = new EarObs() { EventId = "123456789" };
obs.icaos = new List<icao>();
obs.icaos.Add(new icao() { icaoID = "0001" });
obs.icaos[0].frms = new List<frm>();
obs.icaos[0].frms.Add(new frm() { frmID = "01" });
obs.icaos[0].frms[0].stas = new List<sta>();
obs.icaos[0].frms[0].stas.Add(new sta() { station = "00", db = 87, hz = 99 });
obs.icaos[0].frms[0].stas.Add(new sta() { station = "01", db = 79, hz = 99 });
obs.icaos[0].frms.Add(new frm() { frmID = "02" });
using (StringWriter s = new StringWriter())
{
serializer.Serialize(s, obs);
string test = s.ToString();
}
Outputs;
<?xml version="1.0" encoding="utf-16"?>
<EarObs xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" EventId="123456789">
<icao icaoID="0001">
<frm frm="01">
<sta station="00">
<db>87</db>
<hz>99</hz>
</sta>
<sta station="01">
<db>79</db>
<hz>99</hz>
</sta>
</frm>
<frm frm="02" />
</icao>
</EarObs>
Now, while this seems like a lot of trouble to go to, it's possible to use the xsd.exe tool (comes with the framework I believe), to automatically create a set of classes that match any given xml file, although it does use an intermediary xsd file (painless though). You can find out how here; How to generate .NET 4.0 classes from xsd?
I have created new class to read the data from xml file, which looks like :
public class Validations
{
public string id { get; set; }
public List<string> lhsList { get; set; }
public List<string> rhsList { get; set; }
}
XML I am trying to read is:
<root>
<Validation id="val3">
<lhs id='Estimated' />
<lhs id='Newqurter' />
<rhs id='Current' />
<rhs id='FirstQuarter' />
</Validation>
.
.
.
</root>
Code I have written to read the xml is :
List<Validations> vList = new List<Validations>();
vList = (from XElement xele in xdoc.Root.Elements()
select new Validations
{
id = xele.Attribute("id").Value.ToString(),
// lhsList = ((xele.Elements().FirstOrDefault(p => p.Name == "lhs").FirstAttribute.Value
// rhsList = ((xele.Elements().FirstOrDefault(p => p.Name == "rhs").FirstAttribute.Value
}
).ToList<Validations>();
How do read the List<lhsList> ?
I tried
lhsList = ((xele.Elements().FirstOrDefault(p => p.Name == "lhs").FirstAttribute.Value).ToList(),
But its not working as expected. What can be other ways to do this?
You can create the list of lhs elements as follows:
List<string> lhsElements = xele.Elements("lhs")
.Select(el => el.Attribute("id").Value)
.ToList();
This selects all the lhs elements that are children of xele, then selects the value of their 'id' attribute. I'll leave it to you to work out how to merge this with your code.
I have products in a class that I want to save in a XML file, the products is devided into categoris and I would like to save my products in a XML file formatet like below examlple. Products for category 1 writes under category one and products for category 2 under category two.
Hwo can I write a method that dose that?
Thanks in advance.
<Categories>
<Category ID="1">
<CategoryName>Categoriname1</CategoryName>
<Description>drinks, coffees, beers, ales</Description>
<Products>
<Product>
<ProductName>Coffe</ProductName>
<QuantityPerUnit>15 boxes x 60 bags</QuantityPerUnit>
<UnitPrice>25</UnitPrice>
<UnitsInStock>3</UnitsInStock>
<UnitsOnOrder>0</UnitsOnOrder>
</Product>
<Product>
<ProductName>Chang</ProductName>
<QuantityPerUnit>24 - 12 oz bottles</QuantityPerUnit>
<UnitPrice>19</UnitPrice>
<UnitsInStock>17</UnitsInStock>
<UnitsOnOrder>40</UnitsOnOrder>
</Product>
</Products>
</Category>
<Category ID="2">
<CategoryName>Condiments</CategoryName>
<Description>spreads, and seasonings</Description>
<Products>
<Product>
<ProductName>Productname</ProductName>
You can use LINQ to XML:
http://www.hookedonlinq.com/LINQtoXML5MinuteOverview.ashx
You can use LINQ to XML.
your example would start off like...
var root = new XElement("Categories",
new XElement("Category",
new XAttribute("ID",1),
new XElement("CategoryName", "Categoriname1")
)
);
Your intellisense should help you get the rest
One option is to use LINQ to XML. Assume you have these classes (where I have removed some properties to simplify the example):
class Category {
public Int32 Id { get; set; }
public String Name { get; set; }
public IEnumerable<Product> Products { get; set; }
}
public class Product {
public String Name { get; set; }
}
You can create some test data:
var categories = new[] {
new Category {
Id = 1,
Name = "Category 1",
Products = new[] {
new Product { Name = "Coffee" },
new Product { Name = "Chang" }
}
},
new Category {
Id = 2,
Name = "Condiments",
Products = new[] {
new Product { Name = "Product 1" }
}
}
};
You can then create an XDocument from the test data:
var xmlDocument = new XDocument(
new XElement(
"Categories",
categories.Select(
c => new XElement(
"Category",
new XAttribute("ID", c.Id),
new XElement("CategoryName", c.Name),
new XElement("Products",
c.Products.Select(
p => new XElement(
"Product",
new XElement("ProductName", p.Name)
)
)
)
)
)
)
);
To save it to a file you can use the Save method:
xmlDocument.Save("Categories.xml");
You need to serialize those classes. Create classes marked with Serializable attribute and use XmlSerializer
example:
http://www.dotnetjohn.com/articles.aspx?articleid=173
You could use the XmlSerializer class or the XmlDocument class or the XDocument class or even the XmlTextWriter class.
You may also like to simply serialise the object(s) that you have. You may write a method like this:
public static bool WriteToXMLFile(string fullFileNameWithPath, Object obj, Type ObjectType)
{
TextWriter xr = null;
try
{
XmlSerializer ser = new XmlSerializer(ObjectType);
xr = new StreamWriter(fullFileNameWithPath);
ser.Serialize(xr, obj);
}
catch (Exception ex)
{
throw ex;
}
finally
{
if(xr != null)
xr.Close();
}
return true;
}