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();
}
Related
I have a XML file which contains about 850 XML nodes. Like this:
<NameValueItem>
<Text>Test</Text>
<Code>Test</Code>
</NameValueItem>
........ 849 more
And I want to add a new Childnode inside each and every Node. So I end up like this:
<NameValueItem>
<Text>Test</Text>
<Code>Test</Code>
<Description>TestDescription</Description>
</NameValueItem>
........ 849 more
I've tried the following:
XmlDocument doc = new XmlDocument();
doc.Load(xmlPath);
XmlNodeList nodes = doc.GetElementsByTagName("NameValueItem");
Which gives me all of the nodes, but from here am stuck(guess I need to iterate over all of the nodes and append to each and every) Any examples?
You need something along the lines of this example below. On each of your nodes, you need to create a new element to add to it. I assume you will be getting different values for the InnerText property, but I just used your example.
foreach (var rootNode in nodes)
{
XmlElement element = doc.CreateElement("Description");
element.InnerText = "TestDescription";
root.AppendChild(element);
}
You should just be able to use a foreach loop over your XmlNodeList and insert the node into each XmlNode:
foreach(XmlNode node in nodes)
{
node.AppendChild(new XmlNode()
{
Name = "Description",
Value = [value to insert]
});
}
This can also be done with XDocument using LINQ to XML as such:
XDocument doc = XDocument.Load(xmlDoc);
var updated = doc.Elements("NameValueItem").Select(n => n.Add(new XElement() { Name = "Description", Value = [newvalue]}));
doc.ReplaceWith(updated);
If you don't want to parse XML using proper classes (i.e. XDocument), you can use Regex to find a place to insert your tag and insert it:
string s = #"<NameValueItem>
<Text>Test</Text>
<Code>Test</Code>
</NameValueItem>";
string newTag = "<Description>TestDescription</Description>";
string result = Regex.Replace(s, #"(?<=</Code>)", Environment.NewLine + newTag);
but the best solution is Linq2XML (it's much better, than simple XmlDocument, that is deprecated at now).
string s = #"<root>
<NameValueItem>
<Text>Test</Text>
<Code>Test</Code>
</NameValueItem>
<NameValueItem>
<Text>Test2</Text>
<Code>Test2</Code>
</NameValueItem>
</root>";
var doc = XDocument.Load(new StringReader(s));
var elms = doc.Descendants("NameValueItem");
foreach (var element in elms)
{
element.Add(new XElement("Description", "TestDescription"));
}
var text = new StringWriter();
doc.Save(text);
Console.WriteLine(text);
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);
}
I need to create an XmlDocument partly by using old XML and partly by creating new. The problem is that the old XML contains custom namespaces and I can't seem to be able to use them as I get an XmlException. I've tried to add the namespace to many different places but I can't get over the Exception!
The Exception
System.Xml.XmlException was unhandled by user code
Message='my' is an undeclared prefix. Line 1, position 42.
Source=System.Xml
My Code
XmlDocument doc = new XmlDocument();
XmlSchema schema = new XmlSchema();
schema.Namespaces.Add("my", "http://foobar.com/");
doc.Schemas.Add(schema);
XmlDeclaration dec = doc.CreateXmlDeclaration("1.0", "UTF-8", null);
doc.AppendChild(dec);
XmlElement root = doc.CreateElement("root");
root.SetAttribute("xmlns:my", "http://foobar.com/");
doc.AppendChild(root);
foreach (var item in GetItems())
{
XmlElement elem = doc.CreateElement("item");
elem.SetAttribute("id", item.id);
// Append body to elem
XmlElement body = doc.CreateElement("body");
body.InnerXml = item.Body; // Here is where I get the exception
elem.AppendChild(body);
// Append elem to root
root.AppendChild(elem);
}
Input from Item.Body is similar to
<aaa><bbb my:attr="55">Foo</bbb></aaa>
I expected the output to be similar to
<?xml version="1.0" encoding="utf-8"?>
<root my:attr="http://foobar.com/">
<item id="12345">
<body>
<aaa>
<bbb my:attr="55">Foo</bbb>
</aaa>
</body>
</item>
</root>
I'm open to alternatives to using this method. After I create the XmlDocument I prettyprint it, validate it against a schema and then push it out for the user to see.
The following is a workaround, best I can come up with:
XNamespace my = "http://foobar.com/";
var doc = new XDocument(new XElement("root",
new XAttribute(XNamespace.Xmlns + "my", my)));
var body = new XElement("body");
doc.Root.Add(new XElement("item", new XAttribute("id", 12345), body));
string innerItem = #"<aaa><bbb my:attr=""55"">Foo</bbb></aaa>";
string itemWrap = #"<wrap xmlns:my=""http://foobar.com/"">" + innerItem + "</wrap>";
XElement item = XElement.Parse(itemWrap);
body.Add(item.Element("aaa"));
Console.WriteLine(doc);
In my previous question here, I didn’t understand how to solve my problem.
Linq to XML, how to acess an element in C#?
Here is my XML I need to parse:
<root>
<photo>/filesphoto.jpg</photo>
<photo:mtime>12</photo:mtime>
<text>some text</text>
</root>
To access the element I use this code:
var doc = XDocument.Parse(xml.Text);
doc.Descendants("text").FirstOrDefault().Value;
How can I access ?
I have try http://aspnetgotyou.blogspot.com/2010/06/xdocument-or-xelement-with-xmlnamespace.html,
But it is ignored <photo:mtime> and I need to access it.
Please write some code.
Contrary to #BrokenGlass' comments, your XML is not invalid. In fact the technique in the link you provided in your question (for loading namespaces) works fine. Maybe you just didn't change the example for your own needs. Here's a more compact generalization for parsing xml fragments with namespaces into an XElement:
public static XElement parseWithNamespaces(String xml, String[] namespaces) {
XmlNamespaceManager nameSpaceManager = new XmlNamespaceManager(new NameTable());
foreach (String ns in namespaces) { nameSpaceManager.AddNamespace(ns, ns); }
return XElement.Load(new XmlTextReader(xml, XmlNodeType.Element,
new XmlParserContext(null, nameSpaceManager, null, XmlSpace.None)));
}
Using your exact input:
string xml =
#"<root>
<photo>/filesphoto.jpg</photo>
<photo:mtime>12</photo:mtime>
<text>some text</text>
</root>";
XElement x = parseWithNamespaces(xml, new string[] { "photo" });
foreach (XElement e in x.Elements()) {
Console.WriteLine("{0} = {1}", e.Name, e.Value);
}
Console.WriteLine(x.Element("{photo}mtime").Value);
Prints:
photo = /filesphoto.jpg
{photo}mtime = 12
text = some text
12
Try this: (Your xml is changed a little, see )
string xml = "<root><photo>/filesphoto.jpg</photo><photoMtime>12</photoMtime><text>some text</text></root>";
var doc = XDocument.Parse(xml);
string value = doc.Descendants("text").FirstOrDefault().Value;
MessageBox.Show(value);
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