Reading XML in 1 line.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Denny Lam
    New Member
    • Jun 2011
    • 5

    Reading XML in 1 line.

    The XML I have is same as web.config where data do not repeat. I want a very handy method to pull data out of the xml.

    What I want is ex:

    1. Response.Write( Xml("\\Roots\No de\Element"))

    2. <% Xml("\\Roots\No de\Element") %>

    I've gone through many example with XMLReader, Xnode, etc.. but they all involve looping over the xml which I don't want. I want to point it directly to the Element and it'll return it's value.

    Is there any build tool I can use to read XML in 1 line?
    How can I read xml in the minimum amount of code?
  • Subin Ninan
    New Member
    • Sep 2010
    • 91

    #2
    Simplest way to deal with xml files is using dataset.

    Comment

    • Denny Lam
      New Member
      • Jun 2011
      • 5

      #3
      I want to avoid using collections. All I want to do is to pull a simple value out from the xml.

      Thanks

      Comment

      • Denny Lam
        New Member
        • Jun 2011
        • 5

        #4
        I guess you're right.
        I wanted to find a build-in method and avoid loading it to memory but DataSet is not that bad.
        Thanks

        ex: xmlFile "XMLFile1.x ml"
        xmlPath "/books/book/Author"

        XML:
        <?xml version="1.0" encoding="utf-8" ?>
        <books>
        <book>
        <Author>AuthorT xt</Author>
        </book>
        </books>
        Code:
        protected string getXmlValue(string xmlFile, string xmlPath)
                {            
                    XmlDocument doc = new XmlDocument();
                    doc.Load(Server.MapPath(xmlFile));
        
                    XmlNode node;
                    XmlElement root = doc.DocumentElement;
                    node = root.SelectSingleNode(xmlPath);                        
                    return (node == null)? "Not found" : node.InnerText;
                }

        Comment

        • Nikola Djokic
          New Member
          • Jun 2011
          • 1

          #5
          For reading XML node, and it's value the best answer is to use XMLPaht:



          Code:
          XDocument xDoc = XDocument.Parse(xml);
                var elem = (from xElem in xDoc.Root.Elements()
                            where xElem.Name == "DemoXML"
                            select xElem).First();
           
                // The best!!!
                // MSDN recommended
                // using System.Xml.XPath;
                var res1 = xDoc.Root.XPathSelectElement("MyData/Name");
                if (res1 != null)
                {
                  res1.Value = "NewValue";
                }
          I hope this helped you!
          Last edited by Curtis Rutland; Jun 14 '11, 03:51 PM.

          Comment

          • Denny Lam
            New Member
            • Jun 2011
            • 5

            #6
            Can you please explain how to use Linq?

            XML:
            <books>
            <book>
            <Author>AuthorT xt</Author>
            <Title>TitleTxt </Title>
            </book>
            </books>

            When I try using your LINQ to look for book it works but when I try to look for Author it crashes.

            Comment

            Working...