c# - Search through XML and grab another Node -


<?xml version="1.0" encoding="utf-8"?> <envelope xmlns:xsd="http://www.w3.org/2001/xmlschema">    <message>       <messageid>1</messageid>       <product>          <sku>33333-01</sku>       </product>    </message> </envelope> 

i've tried googling whether i'm not providing correct search criteria don't know.

i want able search xml file based on messageid , grab sku.

i want search xml file based on sku , remove message completely.

<?xml version="1.0" encoding="utf-8"?> <envelope xmlns:xsd="http://www.w3.org/2001/xmlschema">    <message>       <messageid>1</messageid>       <inventory>          <sku>33333-01</sku>          <quantity>1</quantity>       </inventory>    </message>       <message>       <messageid>2</messageid>       <inventory>          <sku>22222-01</sku>          <quantity>1</quantity>       </inventory>    </message> </envelope> 

meaning xml above becomes:

<?xml version="1.0" encoding="utf-8"?> <envelope xmlns:xsd="http://www.w3.org/2001/xmlschema">    <message>       <messageid>2</messageid>       <inventory>          <sku>22222-01</sku>          <quantity>1</quantity>       </inventory>    </message> </envelope> 

to confirm cannot confirm messageid same on different xml files.

thanks in advance help.

my questions:

  1. how search through xml files?
  2. how grab nodes details
  3. can remove complete xml file based on search?

  1. you can use xmldocument load xml document. then, can use xpath searching nodes.

    xmldocument document = new xmldocument(); document.load("c:\fileonthedisk.xml"); // or document.loadxml("<a>somexmlstring</a>");  // returns single element or null if not found     var singlenode = document.selectsinglenode("envelope/message[messageid = '1']");   // returns nodelist var nodeslist = document.selectnodes("envelope/message[messageid = '1']");  

read more xpath @ w3schools.com.
here xpath tester.

  1. for example, can use following xpath find nodes in document id:

    xmldocument document = new xmldocument(); document.load("c:\doc.xml");  var node = document.selectsinglenode("envelope/message[messageid = '1']");  var sku = node.selectsinglenode("inventory/sku").innertext;  console.writeline("{0} node has sku = {1}", 1, sku); 

or can output skus:

    foreach (xmlnode node in document.selectnodes("envelope/message"))     {         console.writeline("{0} node has sku = {1}",              node.selectsinglenode("messageid").innertext,              node.selectsinglenode("inventory/sku").innertext);     } 

it produce:

    1 node has sku = 33333-01     2 node has sku = 22222-01 

note there possible nullreferenceexceptions if nodes not present.

  1. you can remove using removechild() method of parent.

    xmldocument document = new xmldocument(); document.load("c:\doc.xml");  var node = document.selectsinglenode("envelope/message[messageid = '1']"); node.parentnode.removechild(node);  document.save("c:\docnew.xml"); // without message 1 

Comments

Popular posts from this blog

toolbar - How to add link to user registration inside toobar in admin joomla 3 custom component -

linux - disk space limitation when creating war file -