Click here to Skip to main content
15,907,874 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi.. Friends I have the following xml file...
XML
<?xml version="1.0" standalone="yes"?>
<LiveChat>
  <Detail>
    <Id>1</Id>
    <Adver>STD</Adver>
    <BitVer>32</BitVer>
    <BtnScrn>HLC</BtnScrn>
    <IsLiveChat>Y</IsLiveChat>
  </Detail>
  <Detail>
    <Id>2</Id>
    <Adver>STD</Adver>
    <BitVer>64</BitVer>
    <BtnScrn>HLC</BtnScrn>
    <IsLiveChat>Y</IsLiveChat>
  </Detail>
  <Detail>
    <Id>3</Id>
    <Adver>STD</Adver>
    <BitVer>32</BitVer>
    <BtnScrn>FLC</BtnScrn>
    <IsLiveChat>Y</IsLiveChat>
  </Detail>
  <Detail>
    <Id>4</Id>
    <Adver>STD</Adver>
    <BitVer>64</BitVer>
    <BtnScrn>FLC</BtnScrn>
    <IsLiveChat>Y</IsLiveChat>
  </Detail>  
</LiveChat>


I want to get the value of IsLiveChat node where Adver=STD and BitVer=64 and BtnScrn=FLC, how it is possible in asp page.

Please help me if anybody has some idea or knowledge.

Thanks & Regards
Parveen Rathi
Posted

One of the ways is using XPath

Something like:
HTML
LiveChat/Detail/IsLiveChat[@Adver='STD' and @BitVer='64' and @BtnScrn='FLC']


Details here:
MSDN:XPath Examples[^]
XPath - Elements and Attributes[^]
 
Share this answer
 
Comments
VJ Reddy 16-May-12 2:52am    
Good answer. 5!
Sandeep Mewara 16-May-12 4:04am    
Thanks VJ.
XPath mentioned in Solution 1 by Sandeep Mewara is a good option.

Alternatively LINQ can be used as follows
C#
string xmlText =@"<?xml version=""1.0"" standalone=""yes""?>
<LiveChat>
  <Detail>
    <Id>1</Id>
    <Adver>STD</Adver>
    <BitVer>32</BitVer>
    <BtnScrn>HLC</BtnScrn>
    <IsLiveChat>Y</IsLiveChat>
  </Detail>
  <Detail>
    <Id>2</Id>
    <Adver>STD</Adver>
    <BitVer>64</BitVer>
    <BtnScrn>HLC</BtnScrn>
    <IsLiveChat>Y</IsLiveChat>
  </Detail>
  <Detail>
    <Id>3</Id>
    <Adver>STD</Adver>
    <BitVer>32</BitVer>
    <BtnScrn>FLC</BtnScrn>
    <IsLiveChat>Y</IsLiveChat>
  </Detail>
  <Detail>
    <Id>4</Id>
    <Adver>STD</Adver>
    <BitVer>64</BitVer>
    <BtnScrn>FLC</BtnScrn>
    <IsLiveChat>Y</IsLiveChat>
  </Detail>
</LiveChat>";
XDocument liveChat = XDocument.Parse(xmlText);
//default element created to read if the searched value is not found
XElement defaultDetail = new XElement ("Detail",
            new XElement ("IsLiveChat", "N"));
string isLiveChatVal= (liveChat.Root.Elements().FirstOrDefault (r =>
              r.Element("Adver").Value=="STD" &&
              r.Element("BitVer").Value=="64" &&
              r.Element("BtnScrn").Value=="FLC") ?? defaultDetail)
       .Element("IsLiveChat").Value;

//isLiveChatVal = Y
 
Share this answer
 
v2
Comments
Sandeep Mewara 16-May-12 4:04am    
Yep, another option. Good one. 5!
VJ Reddy 16-May-12 4:22am    
Thank you, Sandeep :)
Parveen Rathi 16-May-12 5:13am    
Thanks sir, but I want to write this code in my asp page, so please tell me how can I achieve this.
VJ Reddy 16-May-12 5:54am    
It can be done in code behind of aspx page.
Thank you.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900