Showing posts with label XML. Show all posts
Showing posts with label XML. Show all posts

Tuesday, March 29, 2011

How to get nested tags using Linq To XML query

Let's say we have the next XML:


This is a link example


  
    
      
        Text...
        Text...
        text...
        Text...
        Text...
      
      
        Text
        Text
        Text
      
      
        Info
        Info
        Info
        Info
      
    
  


And you want to get the tags inside on the chapter number two.
You can use this code:
// Load the document
XDocument loadedData = XDocument.Load("kjv.xml");

// Get the testament
IEnumerable<xelement> xmlTestament = loadedData.Descendants("testament").Where(c => ((string)c.Attribute("name")).Equals("New"));

// Get the book
IEnumerable<xelement> xmlBook      = xmlTestament.Descendants("book").Where(c => ((string)c.Attribute("name")).Equals("Matthew"));

// Get the chapter
IEnumerable<xelement> xmlChapter = xmlBook.Descendants("chapter").Where(c => ((string)c.Attribute("number")).Equals("2"));

foreach (string s in xmlChapter.Descendants("verse"))
{
  listBox1.Items.Add(s);
}

// -----------------------------------------------------------------------------------------


This is another way to get the info needed
//
// Load the document
XDocument loadedData = XDocument.Load("kjv.xml");

// Get the info
IEnumerable<XElement> data1 = from query in loadedData.Descendants("testament")
                                             where (string)query.Attribute("name").Value == "New"
                                        from book in loadedData.Descendants("book")
                                             where (string)book.Attribute("name").Value == "Matthew"
                                        from chap in loadedData.Descendants("chapter")
                                             where (string)chap.Attribute("number").Value == "2"
                                        select chap;

foreach (string s in data1.Descendants("verse"))
{
   listBox1.Items.Add(s);
}


//-----------------------------------------------------------------------------------


Friday, January 14, 2011

XML Programatically make xml tags look aligned

The next code is useful for making your XML tags a little more readable by aligning the tags.



public static String xmlAligned(String Xml)
        {
            String Result = Xml;
            if (Xml.Length != 0)
            {
                try
                {
                    // Load the XmlDocument with the XML variable
                    XmlDocument D = new XmlDocument();
                    D.LoadXml(Xml);
                    MemoryStream MS = new MemoryStream();
                    XmlTextWriter W = new XmlTextWriter(MS, System.Text.Encoding.Unicode);

                    // Format the XML document
                    W.Formatting = Formatting.Indented;

                    // Write the Xml into a formatting XmlTextWriter
                    D.WriteContentTo(W);
                    W.Flush();
                    MS.Flush();

                    // Rewind the MemoryStream in order to read its contents.
                    MS.Position = 0;

                    // Read MemoryStream contents
                    StreamReader SR = new StreamReader(MS);

                    // Get the text
                    String FormattedXml = SR.ReadToEnd();

                    // Get the results
                    Result = FormattedXml;

                    try
                    {
                        // Close all the resources used
                        MS.Close();
                        MS = null;
                        W.Close();
                        W = null;
                    }
                    catch { }
                }
                catch { }
            }

            // Return the aligned XML
            return Result;
        }



Thursday, January 13, 2011

XML reserved characters


Reserved characters in XML

XML reserves certain characters, including less-than (<), greater than (>), and ampersand (&).
To express these characters in your document data, use the equivalent character entity:

less-than (<) &lt;
greater than (>) &gt;
ampersand (&) &amp;

Here is an XML document fragment that uses character entities:


<equation> x &lt; 2 </equation>
      <rhyme> Jack &amp; Jill </rhyme>




Alternatively, data can be protected from the XML parser in a CDATA section.
Use a CDATA section to escape text that would otherwise be recognized as markup.

<comedians>
  <comedian>
    <![CDATA[Abbott & Costello]]>
  </comedian>
 
  <comedian>
    <![CDATA[Laurel & Hardy]]>
  </comedian>
 
  <comedian>
    <![CDATA[Amos & Andy]]>
  </comedian>
</comedians>



Source:
https://studio.tellme.com/general/xmlprimer.html