xml - How can I make changes to an xmldataprovided itemsource in c#? -
i have binded xml file listbox using xmldataprovider in xaml file. possible add or delete items programatically without having convert iobservablecollection, clearing itemssource, making changes , setting itemssource collection?
document property of xmldataprovider standard xmldocument object. can add, remove, or modify item same way xml file abstracted using xmldocument. can find many resources on net explaining how deal xmldocument. so, simple example adapted codeproject article :
the xaml :
<listbox x:name="teamslistbox" margin="0,0,0,20" dockpanel.dock="left" itemssource="{binding}" issynchronizedwithcurrentitem="true" visibility="visible" selectionmode="single"> <listbox.itemtemplate> <datatemplate> <label content="{binding xpath=name}"/> </datatemplate> </listbox.itemtemplate> <listbox.datacontext> <xmldataprovider x:name="teamdata" source="teams.xml" xpath="teams/team" /> </listbox.datacontext> </listbox> the xml :
<teams xmlns:xsi="http://www.w3.org/2001/xmlschema-instance"> <team> <id>1</id> <name>arizona cardinals</name> <conference>nfc west</conference> </team> <team> <id>2</id> <name>atlanta falcons</name> <conference>nfc south</conference> </team> </teams> sample code remove first team programmatically :
var xmldata = (xmldataprovider) teamslistbox.datacontext; var node = xmldata.document.documentelement["team"].selectsinglenode("//team[./id='1']"); node.parentnode.removechild(node); nb: better if provide more context (relevant code have) in future questions, people can focus on answering actual question , can skip process of recreate context.
Comments
Post a Comment