Is it possible to build a Yaml document dynamically from c# with Yaml.DotNet or another library?
I understand how this can be done using serialisation, however that requires starting with an object structure.
I'm looking to find a way to create the Yaml document nodes on the fly as you would with Xml using the XElement.Add(object) method for example.
You can do that using YamlDotNet. You start by creating a YamlStream, add one or more document to it, then you can add sequences, mappings and scalars to it.
Here is an example on how to do it:
var address = new YamlMappingNode(
new YamlScalarNode("street"),
new YamlScalarNode("123 Tornado Alley\nSuite 16") { Style = YamlDotNet.Core.ScalarStyle.Literal },
new YamlScalarNode("city"),
new YamlScalarNode("East Westville"),
new YamlScalarNode("state"),
new YamlScalarNode("KS")
) { Anchor = "main-address" };
var stream = new YamlStream(
new YamlDocument(
new YamlMappingNode(
new YamlScalarNode("repeipt"),
new YamlScalarNode("Oz-Ware Purchase Invoice"),
new YamlScalarNode("date"),
new YamlScalarNode("2007-08-06"),
new YamlScalarNode("customer"),
new YamlMappingNode(
new YamlScalarNode("given"),
new YamlScalarNode("Dorothy"),
new YamlScalarNode("family"),
new YamlScalarNode("Gale")
),
new YamlScalarNode("items"),
new YamlSequenceNode(
new YamlMappingNode(
new YamlScalarNode("part_no"),
new YamlScalarNode("A4786"),
new YamlScalarNode("descrip"),
new YamlScalarNode("Water Bucket (Filled)"),
new YamlScalarNode("price"),
new YamlScalarNode("1.47"),
new YamlScalarNode("quantity"),
new YamlScalarNode("4")
),
new YamlMappingNode(
new YamlScalarNode("part_no"),
new YamlScalarNode("E1628"),
new YamlScalarNode("descrip"),
new YamlScalarNode("High Heeled \"Ruby\" Slippers"),
new YamlScalarNode("price"),
new YamlScalarNode("100.27"),
new YamlScalarNode("quantity"),
new YamlScalarNode("1")
)
),
new YamlScalarNode("bill-to"), address,
new YamlScalarNode("ship-to"), address,
new YamlScalarNode("specialDelivery"),
new YamlScalarNode("Follow the Yellow Brick\n" +
"Road to the Emerald City.\n" +
"Pay no attention to the\n" +
"man behind the curtain.")
{
Style = YamlDotNet.Core.ScalarStyle.Literal
}
)
)
);
I've now worked out how to do this using Yaml.Net. The YamlStream needs to be loaded with some initial content using the Load() method.
const string initialContent = "---\nversion: 1\n...";
var sr = new StringReader(initialContent);
var stream = new YamlStream();
stream.Load(sr);
You can then cast the RootNode of the YamlDocument to YamlMappingNode which has an Add method.
var rootMappingNode = (YamlMappingNode)stream.Documents[0].RootNode;
rootMappingNode.Add("shout", "yay!");
You can then add a variety of node types before saving:
var props = new YamlMappingNode();
props.Add("prop1", "value1");
props.Add("prop2", "value2");
rootMappingNode.Add("itemWithProps", props);
var props2 = new YamlMappingNode();
props2.Add("prop1", "value1");
props2.Add("prop2", "value2");
var props3 = new YamlMappingNode();
props3.Add("prop1", "value1");
props3.Add("prop2", "value2");
var seq = new YamlSequenceNode();
seq.Add(props2);
seq.Add(props3);
rootMappingNode.Add("sequenceOfItems", seq);
var col = new YamlSequenceNode();
col.Style = SequenceStyle.Flow;
col.Add("a");
col.Add("b");
col.Add("c");
var seqMapping = new YamlMappingNode();
seqMapping.Add("collection", col);
seq.Add(seqMapping);
using (TextWriter writer = File.CreateText("C:\\temp\\test.yaml"))
stream.Save(writer, false);
The output from this example is:
version: 1
shout: yay!
itemWithProps:
prop1: value1
prop2: value2
sequenceOfItems:
- prop1: value1
prop2: value2
- prop1: value1
prop2: value2
- collection: [a, b, c]
...
Thanks to #Antoine Aubry for creating Yaml.Net and vaguely pointing me in right direction.
Related
I'm trying to create a simple dll runtime, using CodeDOM.
I have quite understand what I need to finish this simple test application.
I need to create with CodeDOM object this statement:
List<string> test = new List<string>() {"A", "B", ... }
I'm just having this statement for declaration of a List of n values, but find nowhere the instructions for reach what I need.
This is my actual code:
CodeCompileUnit compileUnit = new CodeCompileUnit();
CodeNamespace samples = new CodeNamespace("ClassLibrary1");
compileUnit.Namespaces.Add(TestNamespace);
samples.Imports.Add(new CodeNamespaceImport("System"));
samples.Imports.Add(new CodeNamespaceImport("System.Collections.Generic"));
samples.Imports.Add(new CodeNamespaceImport("System.Text"));
CodeTypeDeclaration _class = new CodeTypeDeclaration("TestClass");
CodeMemberField _field = new CodeMemberField();
_field.Attributes = MemberAttributes.Private;
_field.Name = "_testMember";
_field.Type = new CodeTypeReference(typeof(List<string>));
//This is where I cannot understand how to insert the values
_field.InitExpression = new CodeObjectCreateExpression(new CodeTypeReference(typeof(List<string>)), new CodePrimitiveExpression(64));
class1.Members.Add(_field);
How to initialize a list (or an array) with some default values?
Thank you in advance.
As suggested, the answer lies in CodeArrayCreateExpression.
Here is the completed (working) code snippet:
CodeCompileUnit compileUnit = new CodeCompileUnit();
CodeNamespace samples = new CodeNamespace("ClassLibrary1");
compileUnit.Namespaces.Add(samples);
samples.Imports.Add(new CodeNamespaceImport("System"));
samples.Imports.Add(new CodeNamespaceImport("System.Collections.Generic"));
samples.Imports.Add(new CodeNamespaceImport("System.Text"));
CodeTypeDeclaration _class = new CodeTypeDeclaration("TestClass");
CodeMemberField _field = new CodeMemberField();
_field.Attributes = MemberAttributes.Private;
_field.Name = "_testMember";
_field.Type = new CodeTypeReference(typeof(List<string>));
var initialiseExpression = new CodeArrayCreateExpression(
new CodeTypeReference(typeof(string)),
new CodePrimitiveExpression("A"),
new CodePrimitiveExpression("B"),
new CodePrimitiveExpression("C"));
_field.InitExpression = new CodeObjectCreateExpression(new CodeTypeReference(typeof(List<string>)), initialiseExpression);
_class.Members.Add(_field);
The important part is the new initialiseExpression variable that defines the array.
I want to create a JSON file and describe it. The first picture shows how it looks. It should looks like the second picture. Therefore my programming code is wrong. Does anyone have a solution for this? Because when I start my code so it always writes only one, the other things he always overwrites.How do I make it like the second (With the heading (dxfFiles, vipFile) and the structure)?
Thank you for your answers
Pic 1
Pic 2
JObject j = null;
foreach (Detection v in listVIP)
{
j = new JObject(
new JProperty("Dateiname", v.m_sFilename),
new JProperty("Objekt-ID", v.m_sObjectID),
new JProperty("Unterordner", v.m_sSubfolder),
new JProperty("Url-Download", v.m_sDownloadlink)
);
foreach (Detection d in listDXF)
{
j = new JObject(
new JProperty("Dateiname", d.m_sFilename),
new JProperty("Objekt-ID", d.m_sObjectID),
new JProperty("Unterordner", d.m_sSubfolder),
new JProperty("Url-Download", d.m_sDownloadlink)
);
j.Add("Dateiname", "");
}
}
File.WriteAllText(
(Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + "\\test.json"), j.ToString()
);
You can create your desired JSON like this:
JArray jaVIP = new JArray();
foreach (Detection v in listVIP)
{
jaVIP.Add(new JObject(
new JProperty("Dateiname", v.m_sFilename),
new JProperty("Objekt-ID", v.m_sObjectID),
new JProperty("Unterordner", v.m_sSubfolder),
new JProperty("Url-Download", v.m_sDownloadlink)));
}
JArray jaDFX = new JArray();
foreach (Detection d in listDXF)
{
jaDFX.Add(new JObject(
new JProperty("Dateiname", d.m_sFilename),
new JProperty("Objekt-ID", d.m_sObjectID),
new JProperty("Unterordner", d.m_sSubfolder),
new JProperty("Url-Download", d.m_sDownloadlink)));
}
JObject j = new JObject(
new JProperty("dxfFiles", jaDFX),
new JProperty("vipFile", jaVIP));
File.WriteAllText(Environment.GetFolderPath(Environment.SpecialFolder.Desktop) +
"\\test.json", j.ToString());
Fiddle: https://dotnetfiddle.net/RHsKk0
I am trying to build an XML document using Linq, XElement and data from Database,
It's working kinda, but in my XML, I want to close the tag and start a new tag and get the results from the query to populate into for the Tag, it is complaining that my variable r in the tag is unresolved, how can I make this work, or is there a better way of building the XML. All the child elements should be under the parent , having two children and , which has their own set of children.
Here is the code below
public void GenerateXML(int id, string site, string state, string country, string bFn, string bLn, string sFn, string sLn)
{
var results = (from o in _db.Orders
where o.OrderId == id
select o).ToList();
var xmlDoc = new XElement("Order",
from r in results
select
new XElement("OrderHeader",
new XElement("SiteId", site),
new XElement("OrderId", r.OrderId),
new XElement("Time", r.OrderDate.Value),
new XElement("Subtotal", r.SubTotal),
new XElement("Shipping", ""),
new XElement("SalesTax", r.SalesTax),
new XElement("Total", r.Total),
new XElement("PaymentAmount", ""),
new XElement("PaymentMethod", ""),
new XElement("ArchiTypeAcctNum", "20001"),
new XElement("TaxExempt", r.TaxExempt),
new XElement("SpecialInstructions", r.SpecialInstructions),
new XElement("BillTo",
new XElement("BillEmail", r.BillToEmail),
new XElement("FirstName", bFn),
new XElement("LastName", bLn),
new XElement("CompanyName", r.BillCompany),
new XElement("Address1", r.BillToAddress),
new XElement("City", r.BillToCity),
new XElement("State", state),
new XElement("Country", country),
new XElement("Zip", r.BillToZip),
new XElement("Phone", r.BillToPhoneNumber)),
new XElement("ShipTo",
new XElement("FirstName", sFn),
new XElement("LastName", sLn),
new XElement("CompanyName", r.ShipCompany),
new XElement("Address1", r.ShipToAddress),
new XElement("City", r.ShipToCity),
new XElement("State", state),
new XElement("Country", country),
new XElement("Zip", r.ShipToZip),
new XElement("Phone", r.ShipToPhoneNumber))),
new XElement("Items",
from i in r.Items
select new XElement("Item",
new XElement("SKU", i.SkuNumber),
new XElement("PROD_Name", i.ProductName),
new XElement("Description", i.Description),
new XElement("Attributes", i.Attributes),
new XElement("Quantity", i.Quantity),
new XElement("UnitPrice", i.UnitPrice),
new XElement("InkColor", i.InkColor)))
);
xmlDoc.Save(Server.MapPath(#"~/Xml/Orders.xml"));
RedirectToAction("Save");
}
I wrote an extension for same purpose. I think much easier . you can just use as orders.EntityToXml();
public static class XmlExtensions
{
public static bool EntityToXml<T>(this T entity, string filePath)
{
if (string.IsNullOrEmpty(filePath))
{
throw new ArgumentNullException(nameof(filePath));
}
var dir = Path.GetDirectoryName(filePath);
if (string.IsNullOrEmpty(dir))
{
throw new ArgumentNullException(nameof(filePath));
}
if (!Directory.Exists(dir))
{
Directory.CreateDirectory(dir);
}
var serializer= new System.Xml.Serialization.XmlSerializer(typeof(T));
using (var stream = new StreamWriter(filePath))
{
serializer.Serialize(stream , entity);
return true;
}
}
}
How to add the collection of items to XML Document ? Something I started trying, but it's wrong! I'll be grateful for the link on the Internet at a good tutorial too. Also, It is assumed that the iteration will be some elements.
Such code:
public static void Create_Interfaces()
{
XDocument Interfaces;
Interfaces = XDocument.Load("Interfaces.xml");
List<string> intf = new List<string>{"em0","em1","em2"};
foreach (var i in intf)
{
Interfaces = new XDocument(
new XElement("Interfaces",
new XElement("Interface",
new XElement("name", i),
new XElement("vlan-tagging", XElement.EmptySequence),
new XElement("unit",
new XElement("vlan-id", "10"),
new XElement("family", new XElement("inet", new XElement("address", new XElement("name", "10.10.1.23/24"))))))));
}
Interfaces.Save("Interfaces.xml");
}
Get your root element add new elements to it then save it:
var rootElement = Interfaces.Root;
foreach (var i in intf)
{
var element = new XElement("Interface",
new XElement("name", i),
new XElement("vlan-tagging", XElement.EmptySequence),
new XElement("unit",
new XElement("vlan-id", "10"),
new XElement("family",
new XElement("inet",
new XElement("address",
new XElement("name", "10.10.1.23/24"))))));
rootElement.Add(element);
}
rootElement.Save("Interfaces.xml");
I've this generic list
List<zil> listKD = new List<zil>
{
new zil{day="Mon",c="1",S1="08:00",S2="08:40",S3="08:47"},
new zil{day="Mon",c="2",S1="08:50",S2="09:30",S3="09:37"},
new zil{day="Mon",c="3",S1="09:40",S2="10:20",S3="10:27"},
new zil{day="Mon",c="4",S1="10:30",S2="11:10",S3="11:17"},
new zil{day="Tue",c="1",S1="08:00",S2="08:40",S3="08:47"},
new zil{day="Tue",c="2",S1="08:50",S2="09:30",S3="09:37"},
new zil{day="Wed",c="1",S1="08:00",S2="08:40",S3="08:47"},
new zil{day="Wed",c="2",S1="08:50",S2="09:30",S3="09:37"},
new zil{day="Thu",c="1",S1="08:00",S2="08:40",S3="08:47"},
new zil{day="Thu",c="2",S1="08:50",S2="09:30",S3="09:37"},
new zil{day="Thu",c="3",S1="09:40",S2="10:20",S3="10:27"},
new zil{day="Fri",c="1",S1="08:00",S2="08:40",S3="08:47"},
new zil{day="Fri",c="2",S1="08:50",S2="09:30",S3="09:37"},
new zil{day="Fri",c="3",S1="09:40",S2="10:20",S3="10:27"},
new zil{day="Fri",c="4",S1="10:30",S2="11:10",S3="11:17"},
};
and I want save,this list xml but I can't like this xml file
alt text http://aliaydin.com.tr/exXml.jpg
this code doesn't work for I want
XElement zXml = new XElement("Days",
from g in listKD
select new XElement("Day",
new XAttribute("id", g.Day),
new XElement("clock",
new XAttribute("id", g.c),
new XElement("s1", g.s1),
new XElement("s2", g.s2),
new XElement("s3", g.s3)
)));
ZXml.Save("abc.xml");
Thanks...
I think you haven't grouped by day; perhaps something like:
var el = new XElement("Days",
from z in listKD
group z by z.day into tmp
select new XElement("Day",
new XAttribute("id", tmp.Key),
from item in tmp
select new XElement("clock",
new XAttribute("id", item.c),
new XElement("s1", item.S1),
new XElement("s2", item.S2),
new XElement("s3", item.S3))
));
string s = el.ToString(); // or save etc
Update re comment; to reverse it, something like:
XElement parsed = XElement.Parse(s);
var newList = (from day in parsed.Elements("Day")
from clock in day.Elements("clock")
select new zil
{
day = (string)day.Attribute("id"),
c = (string)clock.Attribute("id"),
S1 = (string)clock.Element("S1"),
S2 = (string)clock.Element("S2"),
S3 = (string)clock.Element("S3")
}).ToList();
To filter to a specific day:
from day in parsed.Elements("Day")
where (string)day.Attribute("id") == "Mon"
from clock in day.Elements("clock")
...
[XmlArray("Zils"), XmlArrayItem("Zil", typeof(zil))]
public List<zil> listKD = new List<zil>();