I am developing a universal windows app on windows 10 with Visual Studio 2015 and have a pretty large Xml structured like this:
<header id = "1"> <title> some text </title> <question> a question </question> <user_input> <input1> </input1> <input2> </input2> </user_input> </header> <header id = "2"> <title> some text </title> <question> a question </question> <user_input> <input1> </input1> <input2> </input2> </user_input> </header> ... This is repeating many times. There are parts that should never be changed (e.g. title, question). Now i want to write new elements into "ui", so it can be read again and shows the new content in texbox. I use a FileStream and XmlDocument and XmlNodeList to read the Xml and show the content on textblocks:
path = "test.xml"; FileStream stream = new Filestream(path, FileMode.Open, FileAcces.Read); XmlDocument xdoc = new XmlDocument(); xdoc.Load(reader); XmlNodeList node = xdoc.GetElementsByTagName("header"); textblock1.Text = node[0].Attributes["id"].Value; textblock2.Text = node[i].ChildNode[1].InnerText; .... I tried this to write into the Xml:
XDocument xdoc = XDocument.Load(path); XElement ele = xdoc.Element("header"); ele.Add(new XElement("user_input", new XElement("input1", newtext))); xdoc.Save(path); <---- at this point there is an error "Argument 1: cannot convert from 'string' to 'System.IO.Stream'"
My question is: how can i write the user input (some string) to the place I want it to be? The first input shall be written into header with id = 1 into user_input, the second into header id = "2" and so on. I already tried to load the xml with XDocument and write a new element with XElement, but it work at all.Is there something wrong with my xml? Or is it the function? Thank you in advance.
No comments:
Post a Comment