add Xml in C#
-
Thx a lot! But their is still a mistake i can't find. XmlNode parisNode = countryDocument.SelectSingleNode("/Country/City[@Name 'Paris']"); An unhandled exception of type 'System.Xml.XPath.XPathException' occurred in system.xml.dll Additional information: System error. Do you know what is wrong with the SelectSingleNode? Thx
opps typo by me. mised the equals out!
XmlNode parisNode = countryDocument.SelectSingleNode("/Country/City[@Name = 'Paris']");
"When the only tool you have is a hammer, a sore thumb you will have."
-
opps typo by me. mised the equals out!
XmlNode parisNode = countryDocument.SelectSingleNode("/Country/City[@Name = 'Paris']");
"When the only tool you have is a hammer, a sore thumb you will have."
Sorry to disturb you again but there is another mistake :-) He puts this line in yellow : parisNode.AppendChild(newStreetNode); An unhandled exception of type 'System.NullReferenceException' occurred in CountryAddEdit.exe Additional information: Object reference not set to an instance of an object. And is it possible to put Paris in a string so if i want to change another city i just have to change the string? Thx!
-
Sorry to disturb you again but there is another mistake :-) He puts this line in yellow : parisNode.AppendChild(newStreetNode); An unhandled exception of type 'System.NullReferenceException' occurred in CountryAddEdit.exe Additional information: Object reference not set to an instance of an object. And is it possible to put Paris in a string so if i want to change another city i just have to change the string? Thx!
this is because the SelectSingleNode is failing. might be either your code or the xml file - can you post them. the SelectSingleNode use a plain string, so you can use what ever you want for it...
"When the only tool you have is a hammer, a sore thumb you will have."
-
this is because the SelectSingleNode is failing. might be either your code or the xml file - can you post them. the SelectSingleNode use a plain string, so you can use what ever you want for it...
"When the only tool you have is a hammer, a sore thumb you will have."
string filename = @"c:\country.xml"; XmlDocument countryDocument = new XmlDocument(); countryDocument.Load(filename); XmlNode parisNode = countryDocument.SelectSingleNode("/Country/City[@Name = 'Paris']"); XmlNode newStreetNode = countryDocument.CreateNode(XmlNodeType.Element,"Street",null); XmlAttribute newStreetNameAttr = countryDocument.CreateAttribute("Name"); newStreetNameAttr.Value = "my new street"; newStreetNode.Attributes.Append(newStreetNameAttr); XmlNode newStreetNameNode = countryDocument.CreateNode(XmlNodeType.Element,"Name",null); newStreetNameNode.InnerText = " Test 4 "; newStreetNode.AppendChild(newStreetNameNode); XmlNode newStreetAgeNode = countryDocument.CreateNode(XmlNodeType.Element,"Age",null); newStreetAgeNode.InnerText = " 42 "; newStreetNode.AppendChild(newStreetAgeNode); parisNode.AppendChild(newStreetNode); countryDocument.Save(filename);
countryDocument.CreateNode(XmlNodeType.Element,"Name",null); I found this in the MSDN because there was a mistake there to but did i fill it wrong? -
string filename = @"c:\country.xml"; XmlDocument countryDocument = new XmlDocument(); countryDocument.Load(filename); XmlNode parisNode = countryDocument.SelectSingleNode("/Country/City[@Name = 'Paris']"); XmlNode newStreetNode = countryDocument.CreateNode(XmlNodeType.Element,"Street",null); XmlAttribute newStreetNameAttr = countryDocument.CreateAttribute("Name"); newStreetNameAttr.Value = "my new street"; newStreetNode.Attributes.Append(newStreetNameAttr); XmlNode newStreetNameNode = countryDocument.CreateNode(XmlNodeType.Element,"Name",null); newStreetNameNode.InnerText = " Test 4 "; newStreetNode.AppendChild(newStreetNameNode); XmlNode newStreetAgeNode = countryDocument.CreateNode(XmlNodeType.Element,"Age",null); newStreetAgeNode.InnerText = " 42 "; newStreetNode.AppendChild(newStreetAgeNode); parisNode.AppendChild(newStreetNode); countryDocument.Save(filename);
countryDocument.CreateNode(XmlNodeType.Element,"Name",null); I found this in the MSDN because there was a mistake there to but did i fill it wrong?I should have just used CreateElement :) however, you can use CreateNode, but you don't need the null as we don't need an xml namespace. I suspect the problem is your xml file, can you post that up.
"When the only tool you have is a hammer, a sore thumb you will have."
-
I should have just used CreateElement :) however, you can use CreateNode, but you don't need the null as we don't need an xml namespace. I suspect the problem is your xml file, can you post that up.
"When the only tool you have is a hammer, a sore thumb you will have."
-
yes, I thought so, you have changed the case of "city" in your xml... so either change the SelectNode to be
SelectSingleNode("/Country/city[@Name = 'Paris']");
or fix you xml to be "City".. rememeber XML is case sensitive, so its best to pick on style and stick to it..
"When the only tool you have is a hammer, a sore thumb you will have."
-
yes, I thought so, you have changed the case of "city" in your xml... so either change the SelectNode to be
SelectSingleNode("/Country/city[@Name = 'Paris']");
or fix you xml to be "City".. rememeber XML is case sensitive, so its best to pick on style and stick to it..
"When the only tool you have is a hammer, a sore thumb you will have."
-
well done :-) the other problem:
XmlNode newStreetNode = countryDocument.CreateNode(XmlNodeType.Element,"Street",null);
I changed it intoXmlNode newStreetNode = countryDocument.CreateNode(XmlNodeType.Element,"Street","");
Now it works :-D Thx!or you can use the short version:
XmlNode newStreetNode = countryDocument.CreateElement("Street");
"When the only tool you have is a hammer, a sore thumb you will have."
-
or you can use the short version:
XmlNode newStreetNode = countryDocument.CreateElement("Street");
"When the only tool you have is a hammer, a sore thumb you will have."