c# - Appending/Listing LINQ to XML data -


i'm writing function save , read data to/from , xml document through linq. can write document fine, if go add data existing item, adds new item. goal create address book type system (yes know there's 1000 out there, it's learning project myself) , i've tried ini , basic text seems xml best way go short of using local db sql. have:

    xdocument doc = xdocument.load(@"c:\textxml.xml");     var data = new xelement("entry",     new xelement("name", textbox1.text),     new xelement("address", richtextbox2.text),     new xelement("comments", richtextbox1.text));     doc.element("contacts").add(data);     doc.save(@"c:\textxml.xml"); 

i searched so , can't seem find how append/replace. saves properly, when add document, if want update entry i'm not sure how without creating new "entry" nor have gotten knack of removing one. (i'm new c# still , self-taught pardon obvious i've overlooked.)

my second issues revolves around loading information textboxes. i'm able load list of entry names listbox, when go open information entry i'm not sure how nested info. example above i'd need similar following:

xdocument doc = xdocument.load(@"c:\textxml.xml"); boxname.text = name selecteditem of list box. boxaddress.text = address child of element named above etc. 

each method i've tried wind null reference exception, tells me i'm not pointing right thing, i'm not sure how things properly. i've tried creating string , var of selecteditem list box naming, , using tostring methods, still can't figure out.

for replacing values, there several functions can use in xelement:

value (property public setter) setvalue() setelementvalue() setattributevalue() replacewith() replacenodes() 

for example, if wanted replace value in name:

data.element("name").setvalue("newvalue"); 

or

data.element("name").value = "newvalue"; 

for loading, once have xelement node desire, it's simple doing

xelement.value 

or if it's attribute:

xelement.attribute("attributename").value 

using code example:

boxname.text = doc.element("entry").element("name").value; 

edit address comment:

if i'm reading comment right, you're wanting extract name/address/etc. data all nodes within contacts main node?

if so, want this:

boxname.text = string.join(",", doc.elements("entry").select(x => x.element("name").value)); 

this give single string has names in entries separated comma. change "name" "address" same addresses.

i'd suggest doing search linq xml finding more information how use parsing.


Comments

Popular posts from this blog

java - Jmockit String final length method mocking Issue -

asp.net - Razor Page Hosted on IIS 6 Fails Every Morning -

c++ - wxwidget compiling on windows command prompt -