Writing XML in loop c# - c#

How would i write the xml out like
<?xml version="1.0" encoding="UTF-8"?>
<calibration>
<ZoomLevel 250>0.0100502512562814</ZoomLevel 250>
<ZoomLevel 250>0.0100502512562814</ZoomLevel 250>
........
</calibration>
I know how to write it out but i cant write it out in a loop which i need to atm the i have for writting the xml sheet is
public void XMLWrite(Dictionary<string, double> dict)
{
//write the dictonary into an xml file
XmlDocument doc = new XmlDocument();
XmlNode docNode = doc.CreateXmlDeclaration("1.0", "UTF-8", null);
doc.AppendChild(docNode);
XmlNode productsNode = doc.CreateElement("calibration");
doc.AppendChild(productsNode);
foreach (KeyValuePair<string, double> entry in dict)
{
XmlNode zoomNode = doc.CreateElement("ZoomLevel");
XmlAttribute ZoomLevel = doc.CreateAttribute(entry.Key.ToString());
//XmlElement PixelSize = doc.CreateElement (entry.key = entry.Value.ToString());
zoomNode.Attributes.Append(ZoomLevel);
productsNode.AppendChild(zoomNode);
}
doc.Save(pathName);
}

As the others said your wanted xml isn't valid. Another thing that I noticed is that in your example there are two nodes with the level zoom of 250 which is a key of the dictionary and as you know it should be unique.
However I recommend you to use LINQ to XML (System.Xml.Linq) which is simpler, so what about:
public void XMLWrite( Dictionary<string, double> dict ) {
//LINQ to XML
XDocument doc = new XDocument( new XElement( "calibration" ) );
foreach ( KeyValuePair<string, double> entry in dict )
doc.Root.Add( new XElement( "zoom", entry.Value.ToString( ), new XAttribute( "level", entry.Key.ToString( ) ) ) );
doc.Save( pathName );
}
I tested this code by passing this dictionary:
"250", 0.110050251256281
"150", 0.810050256425628
"850", 0.701005025125628
"550", 0.910050251256281
And the result is:
<?xml version="1.0" encoding="utf-8"?>
<calibration>
<zoom level="250">0,110050251256281</zoom>
<zoom level="150">0,810050256425628</zoom>
<zoom level="850">0,701005025125628</zoom>
<zoom level="550">0,910050251256281</zoom>
</calibration>

As Michiel pointed out in the comments, the XML you want to create is not valid. As of the W3C XML specification:
Almost all characters are permitted in names, except those which
either are or reasonably could be used as delimiters.
You might want to generate something like this instead:
<?xml version="1.0" encoding="UTF-8"?>
<calibration>
<zoom level="250">0.0100502512562814</zoom>
<zoom level="260">0.0100502512562815</zoom>
</calibration>
Generated with such a code snippet:
foreach (KeyValuePair<string, double> entry in dict)
{
var node = doc.CreateElement("zoom");
var attribute = doc.CreateAttribute("level");
attribute.Value = entry.Key;
node.InnerText = entry.Value.ToString(CultureInfo.InvariantCulture);
node.Attributes.Append(attribute);
productsNode.AppendChild(node);
}

Related

Write XML with XDocument doesn't add XElements probably

I am new to C# and Linq. I am trying to write my Dictionary to file as XML. Therefore I use a foreach loop add all XElements to a root XElement. This is added to the document. But it does only add the last iteration of my loop to the document. What am I doing wrong?
Here is the code
public void ToXml(string xmlFile)
{
//Dictionary used: Values
XDocument doc = new XDocument(
new XDeclaration("1.0", "utf-8", null));
XElement xRoot = new XElement("RootElement");
doc.Add(xRoot);
Dictionary<double, double[]>.KeyCollection keys = Values.Keys;
foreach(double key in keys)
{
XElement inner = new XElement("InnerElement",
new XAttribute("value", key),
new XElement("TestValue1", Values[key][0]),
new XElement("Testvalue2", Values[key][1]),
new XElement("TestValue3", Values[key][2]),
new XElement("TestValue4", Values[key][3]),
new XElement("TestValue5", Values[key][4]),
new XElement("TestValue6", Values[key][5]));
xRoot.Add(inner);
}
doc.Save(xmlFile);
}
This is the output:
<?xml version="1.0" encoding="utf-8"?>
<RootElement>
<InnerElement value="400">
<TestValue1>0</TestValue1>
<Testvalue2>0</Testvalue2>
<TestValue3>200</TestValue3>
<TestValue4>0</TestValue4>
<TestValue5>100</TestValue5>
<TestValue6>491</TestValue6>
</InnerElement>
</RootElement>

XElement XML ToString malformed [ ]

So I m parsing some xml. I create my XElement and run the ToString() method. I look at the results and they are all wrong.
<root>[elementName, ElementValue ]</root>
When It should look like
<root>
<data name="Name">
<value>Value</value>
</data>
</root>
This is really weird. I've used XML plenty of times and have never run into this. I have looked on the web and I can't find anything. When I step through the XDocument creation process the ToString() for each element is correct. What is going on? How can I troubleshoot this?
Here is my code
string WriteXml(Dictionary<string, string> dic)
{
var root = new XElement("root");
foreach (var pair in dic)
{
var element = new XElement("data", pair.Value);
element.Add(new XAttribute("name", pair.Key));
root.Add(pair);
}
var doc = new XDocument(new XDeclaration("1.0", "utf-8", null), root);
var s = doc.ToString();
Console.WriteLine(s);
return doc.ToString();
}

How to create a dictionary and store values for the values in c#

I want to create a dictionary for the following xml:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<groups>
<group>
<data>Open</data>
<aggregation>5</aggregation>
</group>
</groups>
I want my dictionary to get the values as:
Open,5
Please note that 'Open' is fetched from <data>Open</data> and '5' is fetched from <aggregation>5</aggregation>.
My current code is as follows:
foreach (XmlNode group in Bugsagg)
{
XmlNode data = group.SelectSingleNode(".//data");
XmlNode aggregate = group.SelectSingleNode(".//aggregation");
if (Dict_Aggregate.ContainsKey(data.InnerText))
{
Dict_Aggregate[data.InnerText]++;
}
else
{
Dict_Aggregate.Add(data.InnerText, 1);
}
I am not getting the desired response. Please suggest where i am doing wrong.Thanks
Use XElement and LINQ to XML.
You should add
using System.Xml;
using System.Xml.Linq;
on top of your code. Then use the following
string xml = #"<?xml version=""1.0"" encoding=""UTF-8"" standalone=""yes""?><groups><group><data>Open</data><aggregation>5</aggregation></group></groups>";
XElement xe = XElement.Parse(xml);
Dictionary<string,string> d =
xe.Elements("group")
.ToDictionary
(
x=>(string)x.Element("data"), //Key Selector
z=>(string)z.Element("aggregation")//Value Selector
);
Some people might also suggest to use an XDocument as you provide a fully qualified xml with declaration etc:
XDocument xd = XDocument.Parse(xml);
Dictionary<string,string> d =
xd.Root.Elements("group")
.ToDictionary
(
x=>(string)x.Element("data"), //Key Selector
z=>(string)z.Element("aggregation")//Value Selector
);

Keeping Redundant Namespace Prefixes On XML Elements in C#

I'm trying to write an XML file that will be picked up and parsed by another service. In order for this to happen the XML must be formatted in a very specific way, namely:
<?xml version="1.0"?>
<Feedbacks:Feedbacks xmlns:Feedbacks="Feedbacks">
<Feedbacks:Elements>
<Feedback:XMLFeedback xmlns:Feedback="Feedback">
<Feedback:MfgUnitID></Feedback:MfgUnitID>
<Feedback:MachineId></Feedback:MachineId>
<Feedback:OperationCode></Feedback:OperationCode>
<Feedback:ItemSeqNum></Feedback:ItemSeqNum>
<Feedback:OperDispositionCd></Feedback:OperDispositionCd>
<Feedback:ItemId></Feedback:ItemId>
<Feedback:ParentItemId></Feedback:ParentItemId>
<Feedback:ItemEndSize>1821</Feedback:ItemEndSize>
<Feedback:ItemDispositionCd></Feedback:ItemDispositionCd>
<Feedback:OperStartDate></Feedback:OperStartDate>
<Feedback:OperEndDate></Feedback:OperEndDate>
</Feedback:XMLFeedback>
</Feedbacks:Elements>
</Feedbacks:Feedbacks>
with data of course between the innermost elements. Here's the issue though, no matter what I do, I can't get any of the C# classes to keep the semicolons on the innermost nodes. As far as I know these need to stay, so is there a way in C# to force it to format the nodes this way? I've tried all of the create methods that I could find in the XMLDocument class. I can get the outer nodes formatted fine, but the inner ones just keep creating problems.
Edit, sorry here's the code that makes the inner nodes.
private void AppendFile(string filename, string[] headers, Dictionary<string, string> values)
{
XmlDocument doc = new XmlDocument();
doc.Load(filename);
XmlNode node = doc.GetElementsByTagName(headers[headers.Length - 2]).Item(0);
string[] hPieces = headers[headers.Length - 1].Split(':');
XmlElement appendee = doc.CreateElement(hPieces[0].Trim(), hPieces[1].Trim(), hPieces[0].Trim());
node.AppendChild(appendee);
foreach (KeyValuePair<string, string> pair in values)
{
string[] ePieces = pair.Key.Split(':');
//XmlElement element = doc.CreateElement(ePieces[0].Trim(), string.Empty, ePieces[1].Trim());
//XmlText text = doc.CreateTextNode(pair.Value);
XmlNode innerNode = doc.CreateNode(XmlNodeType.Element, ePieces[1].Trim(), ePieces[0].Trim());
node.InnerText = pair.Value;
// element.AppendChild(text);
appendee.AppendChild(innerNode);
}
doc.Save(filename);
}
The data for the inner nodes comes in as key value pairs in the dictionary. Where the keys contain the intended name.
Edit2: This is what the file output looks like
<?xml version="1.0" encoding="utf-8"?>
<Feedbacks:Feedbacks xmlns:Feedbacks="Feedbacks">
<Feedbacks:Elements>
<Feedback:XMLFeedback xmlns:Feedback="Feedback">
<MfgUnitID></MfgUnitID>
<MachineId></MachineId>
<OperationCode</OperationCode>
<ItemSeqNum></ItemSeqNum>
<OperDispositionCd></OperDispositionCd>
<ItemId></ItemId>
<ParentItemId></ParentItemId>
<ItemEndSize></ItemEndSize>
<ItemDispositionCd></ItemDispositionCd>
<OperStartDate></OperStartDate>
<OperEndDate></OperEndDate>
</Feedback:XMLFeedback>
</Feedbacks:Elements>
</Feedbacks:Feedbacks>
You can accompish this easily with XLinq:
using System.Xml.Linq;
XNamespace ns1 = "Feedbacks";
XNamespace ns2 = "Feedback";
var doc = new XElement("Feedbacks",
new XAttribute(XNamespace.Xmlns+"Feedbacks", ns1));
doc.Add(new XElement(ns1 + "Elements",
new XElement(ns2 + "Feedback",
new XAttribute(XNamespace.Xmlns+"Feedback", ns2),
new XElement(ns2 + "Unit"))));
Gives
<Feedbacks xmlns:Feedbacks="Feedbacks">
<Feedbacks:Elements>
<Feedback:Feedback xmlns:Feedback="Feedback">
<Feedback:Unit />
</Feedback:Feedback>
</Feedbacks:Elements>
</Feedbacks>
Although I believe that your own output should be valid XML, relying on the parent namespcae.

Converting an XML-document to a dictionary

I do not need to edit any XML-file or anything, this is only for reading and parsing.
I want to be able to handle the XML-document as a dictionary, like: username = doc["username"];, but I can't find out how to "convert" the document. I've also encountered the problem with duplicate key-names, but that could be easlily avoided by appending each value with 1, 2 etc; making it easy to for-loop through too.
Is this possible? To treat the (parsed) XML-document as a dictionary?
Answer to Mehrdad:
It varies from time to time, it depends on the request from the user. If the user requests x, then it will be:
<xml>
<test>foo</test>
<bar>123</bar>
<username>foobar</username>
</xml>
But if he requests y, it will be like
<xml>
<ammount>1000</ammount>
<mail>...#...</mail>
<username>foobar</username>
</xml>
The best would be if this:
<xml>
<mengde>100</mengde>
<type>3</type>
<mail>foo</mail>
<crypt>bar</crypt>
<username>bar</username>
</xml>"
Could be parsed and then accessed as doc["mengde"] etc.
You could use linq to xml to do what you want (if I understand what you want)
string data = "<data><test>foo</test><test>foobbbbb</test><bar>123</bar><username>foobar</username></data>";
XDocument doc = XDocument.Parse(data);
Dictionary<string, string> dataDictionary = new Dictionary<string, string>();
foreach (XElement element in doc.Descendants().Where(p => p.HasElements == false)) {
int keyInt = 0;
string keyName = element.Name.LocalName;
while (dataDictionary.ContainsKey(keyName)) {
keyName = element.Name.LocalName + "_" + keyInt++;
}
dataDictionary.Add(keyName, element.Value);
}
XML Data
<?xml version="1.0" encoding="UTF-8"?>
<data>
<resource key="123">foo</resource>
<resource key="456">bar</resource>
<resource key="789">bar</resource>
</data>
Conversion Code
string s = "<data><resource key=\"123\">foo</resource><resource key=\"456\">bar</resource><resource key=\"789\">bar</resource></data>";
XmlDocument xml = new XmlDocument();
xml.LoadXml(s);
XmlNodeList resources = xml.SelectNodes("data/resource");
SortedDictionary<string,string> dictionary = new SortedDictionary<string,string>();
foreach (XmlNode node in resources){
dictionary.Add(node.Attributes["key"].Value, node.InnerText);
}
This question was asked before here and so you can find the all answers in this link :
convert xml to sorted dictionary
Hope it helps.
Your question's really not very clear, but I think this does what you want:
XmlDocument doc = new XmlDocument();
doc.LoadXml(#"<xml>
<mengde>100</mengde>
<type>2</type>
<foo>bar</foo>
</xml>");
Dictionary<string, string> d = new Dictionary<string, string>();
foreach (XmlNode n in doc.SelectNodes("/xml/*")
{
d[n.Name] = n.Value;
}
This isn't exactly what you are looking for, but may be of interest: http://blogs.msdn.com/csharpfaq/archive/2009/10/01/dynamic-in-c-4-0-introducing-the-expandoobject.aspx

Categories

Resources