How do I get a specific node in an XmlDocument?

Discussion in 'ASP .Net' started by Alan Silver, Jul 13, 2006.

  1. Alan Silver

    Alan Silver Guest

    Hello,

    Sorry if this is an obvious question, but I'm not very familiar with
    using XmlDocument, and I am struggling with a really simple task. I
    couldn't find anything clear that explained what I want to do.

    Suppose I have an XmlDocument like this...

    <books>
    <number>3</number>
    <date>13 July 2006</date>
    <book>
    <title>Fred's big day</title>
    <author>Fred Smith</author>
    <instock>y</instock>
    </book>
    <book>
    <title>Something else</title>
    <author>Someone else</author>
    <instock>n</instock>
    </book>
    <book>
    <title>Big book</title>
    <author>Small fellow</author>
    <instock>y</instock>
    </book>
    </books>

    I would like to list out the titles and authors, with a note as to the
    availability...

    "Fred's big day" by Fred Smith is in stock
    "Something else" by Someone else is NOT in stock
    "Big book" by Small fellow is in stock

    How do I do this? TIA for any help

    --
    Alan Silver
    (anything added below this line is nothing to do with me)
     
    Alan Silver, Jul 13, 2006
    #1
    1. Advertising

  2. Alan Silver

    Guest

    This will do the trick:

    XmlDocument xml = new XmlDocument();

    xml.Load(PathToXMLFile);
    XmlNodeList books = xml.SelectNodes("books/book");

    foreach(XmlNode book in books)
    {
    // for a web app, replace Console.WriteLine with Response.Write
    Console.WriteLine(book.SelectSingleNode("title").InnerText);
    Console.WriteLine(book.SelectSingleNode("author").InnerText);
    Console.WriteLine(book.SelectSingleNode("instock").InnerText);
    }

    Alan Silver wrote:
    > Hello,
    >
    > Sorry if this is an obvious question, but I'm not very familiar with
    > using XmlDocument, and I am struggling with a really simple task. I
    > couldn't find anything clear that explained what I want to do.
    >
    > Suppose I have an XmlDocument like this...
    >
    > <books>
    > <number>3</number>
    > <date>13 July 2006</date>
    > <book>
    > <title>Fred's big day</title>
    > <author>Fred Smith</author>
    > <instock>y</instock>
    > </book>
    > <book>
    > <title>Something else</title>
    > <author>Someone else</author>
    > <instock>n</instock>
    > </book>
    > <book>
    > <title>Big book</title>
    > <author>Small fellow</author>
    > <instock>y</instock>
    > </book>
    > </books>
    >
    > I would like to list out the titles and authors, with a note as to the
    > availability...
    >
    > "Fred's big day" by Fred Smith is in stock
    > "Something else" by Someone else is NOT in stock
    > "Big book" by Small fellow is in stock
    >
    > How do I do this? TIA for any help
    >
    > --
    > Alan Silver
    > (anything added below this line is nothing to do with me)
     
    , Jul 13, 2006
    #2
    1. Advertising

  3. Alan Silver

    Alan Silver Guest

    In article <>,
    writes
    >XmlNodeList books = xml.SelectNodes("books/book");


    Hmm, this is where it all went wrong. I tried using SelectNodes with a
    path, but I never got any nodes returned.

    The real XML file I'm using is much more complex than the one I showed,
    and that is probably my problem. Here is a sample of the real XML
    (pertinent details changed and a fair number of repeated nodes removed
    for brevity). Please could you try it on this and see if you can work
    out how to get an XmlNodeList of the <CoSearchItem> nodes.

    Thanks

    <?xml version="1.0" encoding="UTF-8"?>
    <GovTalkMessage xsi:schemaLocation="blah" xmlns="blah" xmlns:dsig="blah" xmlns:gt="blah" xmlns:xsi="blah">
    <EnvelopeVersion>1.0</EnvelopeVersion>
    <Header>
    <MessageDetails>
    <Class>NameSearch</Class>
    <Qualifier>response</Qualifier>
    <TransactionID>57526921</TransactionID>
    <GatewayTimestamp>2006-07-13T19:35:35-00:00</GatewayTimestamp>
    </MessageDetails>
    <SenderDetails>
    <IDAuthentication>
    <SenderID>removedforsecurity</SenderID>
    <Authentication>
    <Method>CHMD5</Method>
    <Value></Value>
    </Authentication>
    </IDAuthentication>
    <EmailAddress>removedforsecurity</EmailAddress>
    </SenderDetails>
    </Header>
    <GovTalkDetails>
    <Keys />
    </GovTalkDetails>
    <Body>
    <NameSearch xmlns="blah" xmlns:xsi="blah" xsi:schemaLocation="blah">
    <ContinuationKey>removedforsecurity</ContinuationKey>
    <RegressionKey>removedforsecurity</RegressionKey>
    <SearchRows>20</SearchRows>
    <CoSearchItem>
    <CompanyName>PIXALL LTD.</CompanyName>
    <CompanyNumber>SC226107</CompanyNumber>
    <DataSet>LIVE</DataSet>
    <CompanyIndexStatus>DISSOLVED</CompanyIndexStatus>
    <CompanyDate></CompanyDate>
    </CoSearchItem>
    <CoSearchItem>
    <CompanyName>PIXAMEDIA LIMITED</CompanyName>
    <CompanyNumber>05370464</CompanyNumber>
    <DataSet>LIVE</DataSet>
    <CompanyIndexStatus></CompanyIndexStatus>
    <CompanyDate></CompanyDate>
    </CoSearchItem>
    <CoSearchItem>
    <CompanyName>PIX &amp; FIX LOCKSMITHS LIMITED</CompanyName>
    <CompanyNumber>05380781</CompanyNumber>
    <DataSet>LIVE</DataSet>
    <CompanyIndexStatus></CompanyIndexStatus>
    <CompanyDate></CompanyDate>
    </CoSearchItem>
    </NameSearch>
    </Body>
    </GovTalkMessage>

    --
    Alan Silver
    (anything added below this line is nothing to do with me)
     
    Alan Silver, Jul 13, 2006
    #3
  4. Alan Silver

    sloan Guest

    This is a good tutorial that I keep handy.
    It was written way back, but its not overkill for figuring out some simple
    xpath statements.

    http://www.microsoft.com/downloads/...6E-E7A9-4B8C-BFA6-B5BD99F336A5&displaylang=en

    There is one messed up javascript function.. you gotta fix it.

    the function is
    function checkVersion


    //This is already there
    if (sVersion.search(/MSIE 5/) != -1) {
    bReturn = true;
    }
    //ADD THIS NEW CHECK
    if (sVersion.search(/MSIE 6/) != -1) {
    bReturn = true;
    }


    It think you want


    string xpath = "GovTalkMessage/Body/NameSearch/SearchRows/CoSearchItems";

    XmlNodeList books = xml.SelectNodes(xpath);


    xpath IS case sensitive. basically, with Xpath you have to "walk the tree".

    You may have deal with the namespace issue also.

    You can look here
    http://www.sqlservercentral.com/col...eragingxpexcelxmlandopenxmlfordataimports.asp

    for one example of working with namespaces in xml (this is the only one I
    have)
    this is a ~clue, not a great way of telling you how to work with it.

    you're looking for
    SELECT *
    FROM OPENXML (@idoc,
    '/ss:Workbook/ss:Worksheet[position()=1]/ss:Table/ss:Row[position()!=1]',2)
    --//Remove [position()!=1] if there is no header row
    WITH (
    UniqueID varchar(32) './ss:Cell[position()=1]/ss:Data',
    LastName varchar(32) './ss:Cell[position()=2]/ss:Data' ,
    FirstName varchar(32) './ss:Cell[position()=3]/ss:Data' ,
    Street varchar(32) './ss:Cell[position()=4]/ss:Data' ,
    City varchar(32) './ss:Cell[position()=5]/ss:Data' ,
    State varchar(2) './ss:Cell[position()=6]/ss:Data' ,
    Zip varchar(12) './ss:Cell[position()=7]/ss:Data' ,
    Gender char(1) './ss:Cell[position()=8]/ss:Data'

    )



    "Alan Silver" <> wrote in message
    news:p...
    > Hello,
    >
    > Sorry if this is an obvious question, but I'm not very familiar with
    > using XmlDocument, and I am struggling with a really simple task. I
    > couldn't find anything clear that explained what I want to do.
    >
    > Suppose I have an XmlDocument like this...
    >
    > <books>
    > <number>3</number>
    > <date>13 July 2006</date>
    > <book>
    > <title>Fred's big day</title>
    > <author>Fred Smith</author>
    > <instock>y</instock>
    > </book>
    > <book>
    > <title>Something else</title>
    > <author>Someone else</author>
    > <instock>n</instock>
    > </book>
    > <book>
    > <title>Big book</title>
    > <author>Small fellow</author>
    > <instock>y</instock>
    > </book>
    > </books>
    >
    > I would like to list out the titles and authors, with a note as to the
    > availability...
    >
    > "Fred's big day" by Fred Smith is in stock
    > "Something else" by Someone else is NOT in stock
    > "Big book" by Small fellow is in stock
    >
    > How do I do this? TIA for any help
    >
    > --
    > Alan Silver
    > (anything added below this line is nothing to do with me)
     
    sloan, Jul 14, 2006
    #4
  5. Alan Silver

    Alan Silver Guest

    In article <>, sloan
    <> writes

    Sloan,

    Thanks for the reply, comments below...

    >This is a good tutorial that I keep handy.
    >It was written way back, but its not overkill for figuring out some simple
    >xpath statements.

    <snip>
    >It think you want


    Is it? I haven't a clue what to do with it!!! It looks like a way of
    testing functions, but without a deep knowledge of what they are or what
    they are supposed to do, it lost me completely.

    Thanks anyway.

    >string xpath = "GovTalkMessage/Body/NameSearch/SearchRows/CoSearchItems";
    >
    >XmlNodeList books = xml.SelectNodes(xpath);


    OK, that's exactly what I had tried, and failed, before posting. I even
    cut it down to...

    XmlNodeList books = xml.SelectNodes("GovTalkMessage");

    but this didn't return any nodes. I have sat in the Immediate window
    trying all sorts of variations, mainly along the lines of...

    ?xml.SelectNodes("GovTalkMessage").Count

    ....but always get zero returned. This is why I posted.

    Any ideas what's going wrong. Even from my basic understanding of XPath
    I got as far as knowing what should work, it just didn't.

    >xpath IS case sensitive. basically, with Xpath you have to "walk the tree".
    >
    >You may have deal with the namespace issue also.
    >
    >You can look here
    >http://www.sqlservercentral.com/columnists/sholliday/leveragingxpexcelxm
    >landopenxmlfordataimports.asp


    I hate sites that require you to log in to read their content,
    especially ones that admit in advance that you are going to get sent
    e-mails you never asked for.
    <snip>

    Thanks for the reply. Any ideas why my XPath isn't working would be
    appreciated.

    --
    Alan Silver
    (anything added below this line is nothing to do with me)
     
    Alan Silver, Jul 17, 2006
    #5
  6. Alan Silver

    Alan Silver Guest

    Anyone any ideas on this? I'm really stuck.

    In article <>, Alan Silver
    <> writes
    >>string xpath = "GovTalkMessage/Body/NameSearch/SearchRows/CoSearchItems";
    >>
    >>XmlNodeList books = xml.SelectNodes(xpath);

    >
    >OK, that's exactly what I had tried, and failed, before posting. I even
    >cut it down to...
    >
    >XmlNodeList books = xml.SelectNodes("GovTalkMessage");
    >
    >but this didn't return any nodes. I have sat in the Immediate window
    >trying all sorts of variations, mainly along the lines of...
    >
    >?xml.SelectNodes("GovTalkMessage").Count
    >
    >...but always get zero returned. This is why I posted.
    >
    >Any ideas what's going wrong. Even from my basic understanding of XPath
    >I got as far as knowing what should work, it just didn't.


    --
    Alan Silver
    (anything added below this line is nothing to do with me)
     
    Alan Silver, Jul 18, 2006
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    0
    Views:
    1,648
  2. Shoval Tomer
    Replies:
    0
    Views:
    476
    Shoval Tomer
    Jul 9, 2003
  3. Bob
    Replies:
    0
    Views:
    434
  4. Tjerk Wolterink
    Replies:
    2
    Views:
    1,506
    Dimitre Novatchev
    Aug 24, 2006
  5. Replies:
    3
    Views:
    3,933
    A. Bolmarcich
    Oct 20, 2006
Loading...

Share This Page