how to convert String to XML and read this XML???

Last post 07-06-2009 9:49 PM by vickyngo. 12 replies.

Sort Posts:

  • how to convert String to XML and read this XML???

    07-05-2009, 10:01 PM
    • Member
      18 point Member
    • vickyngo
    • Member since 06-10-2009, 5:31 AM
    • Vietnam
    • Posts 49

    If i have a string like this:

    <Order   UserId=’ ‘  CustomerId=’ ’  TotalItem=’ ‘  TotalPrice=’ ‘>

    <OrderLine  ItemId=’ ‘  Quantity=’ ‘  DiscountValue=’ ‘  TaxValue=’ ‘  Price=’ ‘     />

    <OrderLine  ItemId=’ ‘  Quantity=’ ‘  DiscountValue=’ ‘  TaxValue=’ ‘  Price=’ ‘     />

    <OrderLine  ItemId=’ ‘  Quantity=’ ‘  DiscountValue=’ ‘  TaxValue=’ ‘  Price=’ ‘     />

                …………………………………………………………………………………….........

    </Order>

    how do i do to convert above string to xml document

    Vicky

    Please mark this as "Mark as Answer" if it helps you
  • Re: how to convert String to XML ???

    07-05-2009, 11:47 PM
    Answer
    • Contributor
      5,212 point Contributor
    • RickNZ
    • Member since 01-01-2009, 8:43 AM
    • Nelson, New Zealand
    • Posts 863

    Try this:

    using System.Xml;
    
    XmlDocument doc = new XmlDocument();
    doc.LoadXml(yourString);
    


  • Re: how to convert String to XML ???

    07-06-2009, 12:41 AM
    Answer
    • Star
      12,555 point Star
    • malcolms
    • Member since 06-12-2008, 12:38 AM
    • Melbourne, Australia
    • Posts 2,061

    You can use LINQ to XML for that.  Something like:

    string xml = XDocument.Parse("<employee><name>John</name><mobile>363614516</mobile></employee>").ToString(SaveOptions.DisableFormatting);


    Sincerely,
    Malcolm Sheridan

    Microsoft Certified Solution Developer
    Please remember to click "Mark as Answer" on the post that helps you, and to click "Unmark as
    Answer" if a marked post does not actually answer your question.
  • Re: how to convert String to XML ???

    07-06-2009, 12:56 AM

    vickyngo:

    If i have a string like this:

    <Order   UserId=’ ‘  CustomerId=’ ’  TotalItem=’ ‘  TotalPrice=’ ‘>

    <OrderLine  ItemId=’ ‘  Quantity=’ ‘  DiscountValue=’ ‘  TaxValue=’ ‘  Price=’ ‘     />

    <OrderLine  ItemId=’ ‘  Quantity=’ ‘  DiscountValue=’ ‘  TaxValue=’ ‘  Price=’ ‘     />

    <OrderLine  ItemId=’ ‘  Quantity=’ ‘  DiscountValue=’ ‘  TaxValue=’ ‘  Price=’ ‘     />

                …………………………………………………………………………………….........

    </Order>

    how do i do to convert above string to xml document

     string sampleXmlStr = "<Order   UserId=’ ‘  CustomerId=’ ’  TotalItem=’ ‘  TotalPrice=’ ‘>"+

    "<OrderLine  ItemId=’ ‘  Quantity=’ ‘  DiscountValue=’ ‘  TaxValue=’ ‘  Price=’ ‘     />"+

    "<OrderLine  ItemId=’ ‘  Quantity=’ ‘  DiscountValue=’ ‘  TaxValue=’ ‘  Price=’ ‘     />"+

    "<OrderLine  ItemId=’ ‘  Quantity=’ ‘  DiscountValue=’ ‘  TaxValue=’ ‘  Price=’ ‘     />";


            XmlDocument xDoc = new XmlDocument();
            xDoc.LoadXml(sampleXmlStr);
            //In above line...utr string is loaded in to xmlDom. Here if instead of
            //xDoc.LoadXml , u can use xDoc.Load(filePath) if ur xml is in some file.



            //now see this variable.

            string myData = xDoc.OuterXml;

            //In above line u can see ur acutal xml.

    Please mark this post as Answer if it is of help to you!

    " Every wall is a door..! "
  • Re: how to convert String to XML and read this XML???

    07-06-2009, 1:24 AM
    • Member
      18 point Member
    • vickyngo
    • Member since 06-10-2009, 5:31 AM
    • Vietnam
    • Posts 49

    Thanks so much! I want to ask after convert string to xml then to read this xml how i do to read it?

    Vicky

    Please mark this as "Mark as Answer" if it helps you
  • Re: how to convert String to XML and read this XML???

    07-06-2009, 1:51 AM

    vickyngo:

    Thanks so much! I want to ask after convert string to xml then to read this xml how i do to read it?

      string sampleXmlStr = "<Order   UserId='1'  CustomerId='101'  TotalItem='3'  TotalPrice='300'>"+

    "<OrderLine  ItemId='1'  Quantity='1'  DiscountValue='xxx' TaxValue=''  Price='100'/>"+

    "<OrderLine  ItemId='2'  Quantity='1'  DiscountValue='xxx' TaxValue=''  Price='100'/>" +

    "<OrderLine  ItemId='3'  Quantity='1'  DiscountValue='xxx' TaxValue=''  Price='100'/></Order> ";


            XmlDocument xDoc = new XmlDocument();
            xDoc.LoadXml(sampleXmlStr);
            //In above line...utr string is loaded in to xmlDom. Here if instead of
            //xDoc.LoadXml , u can use xDoc.Load(filePath) if ur xml is in some file.


            //now see this variable.

            string myData = xDoc.OuterXml;

            //In above line u can see ur acutal xml.


            //Weel to read this xml...u have to use xmlDom.. see this
           
            //For Example if u want to read the value of Price where userID= "1" and ItemId = 3

            //see the xml above..I have filled dummy values in it.

            XmlNode xNode = xDoc.SelectSingleNode("Order[@UserId = 1]/OrderLine[@ItemId = 3]");
           
            foreach (XmlAttribute xAtt in xNode.Attributes)
            {
                if (xAtt.Name.ToLower() == "price")
                {
                    string myPrice = xAtt.Value;
                }
            }

            //It wii give outpur as myPrice = 100.


            /*FYI...  It is very imporatan to know the xPath queries that can write to deal with xmlDoom
             Here i have written the xQuery at Order[@UserId = 1]/OrderLine[@ItemId = 3]"
             U can google out for more of this...Still if u need more help let me know.
             *
             */

    Please mark this post as Answer if it is of help to you!

    " Every wall is a door..! "
  • Re: how to convert String to XML and read this XML???

    07-06-2009, 2:58 AM
    Answer
    • Member
      18 point Member
    • vickyngo
    • Member since 06-10-2009, 5:31 AM
    • Vietnam
    • Posts 49

    I want to get all attribute, I have code like this:

    XmlDocument xmlDoc = new XmlDocument();
                xmlDoc.LoadXml(xmlBill);

                XmlTextReader reader =
                    new XmlTextReader(new FileStream(xmlDoc, FileMode.Open));
                           
                // Create string builder
                StringBuilder sb = new StringBuilder();

                while (reader.Read())
                {
                    // Check node type
                    switch (reader.NodeType)
                    {
                        case XmlNodeType.Element:
                            sb.Append("Element:");
                            sb.AppendLine(reader.Name);
                            break;

                        default:
                            break;
                    }

                    // Check attributes
                    if (reader.AttributeCount > 0)
                    {
                        if (reader.Name.ToString() == "Order")
                        {
                            string userid = reader.GetAttribute("UserId");
                            string customerid = reader.GetAttribute("CustomerId");
                            string toltalitem = reader.GetAttribute("TotalItem");
                            string totalprice = reader.GetAttribute("TotalPrice");
                        }
                        else if (reader.Name.ToString() == "OrderLine")
                        {
                            string itemid = reader.GetAttribute("ItemId");
                            string quantity = reader.GetAttribute("Quantity");
                            string discountvalue = reader.GetAttribute("DiscountValue");
                            string taxvalue = reader.GetAttribute("TaxValue");
                            string price = reader.GetAttribute("Price");
                        }
                    }
                }

                // Close reader
                reader.Close();


    But when i run it, it's erorr"

    System.ArgumentException: Illegal characters in path.

    "


    Please help me

    Best regards,

    Vicky


    Vicky

    Please mark this as "Mark as Answer" if it helps you
  • Re: how to convert String to XML and read this XML???

    07-06-2009, 4:04 AM

    yeas it shud happen.,. here instead of ItemId = ‘1 u shud there shud be ItemId = '1

    see the differenc in the Quotes i have used..replCE IT. (fyi, ,SEE THE BOLDED OUT PORTION IN ABOV TEXT)


    Please mark this post as Answer if it is of help to you!

    " Every wall is a door..! "
  • Re: how to convert String to XML and read this XML???

    07-06-2009, 4:28 AM
    • Member
      18 point Member
    • vickyngo
    • Member since 06-10-2009, 5:31 AM
    • Vietnam
    • Posts 49

    Hic, my code is still wrong...

    Vicky

    Please mark this as "Mark as Answer" if it helps you
  • Re: how to convert String to XML and read this XML???

    07-06-2009, 4:48 AM
    Answer

    I dont think ur code is wrong.. i meant to say...ur xml is having some bad characters...just like i said in my pre thread..did u check for them?

    Please mark this post as Answer if it is of help to you!

    " Every wall is a door..! "
  • Re: how to convert String to XML and read this XML???

    07-06-2009, 7:34 AM
    Answer
    • Contributor
      5,212 point Contributor
    • RickNZ
    • Member since 01-01-2009, 8:43 AM
    • Nelson, New Zealand
    • Posts 863

    There may be other problems, but at a minimum you should replace your single quotes with double quotes.  Single quotes aren't legal markers for attribute values in XML.

    Have you stepped through the code with a debugger?  That should tell you a lot.


  • Re: how to convert String to XML and read this XML???

    07-06-2009, 7:57 AM
    Answer

     Vicky, either use XmlDocument to parse the XML or use XmlReader to parse it. What you have, new FileStream(xmlDoc), is not meaningful. If you use XmlDocument then forget about XmlReader, you can instead do e.g.

    XmlDocument xmlDoc = new XmlDocument();
    xmlDoc.LoadXml(xmlBill);
    
    XmlElement order = xmlDoc.DocumentElement;
    
    string userid = order.GetAttribute("UserId");
    string customerid = order.GetAttribute("CustomerId");
    string toltalitem = order.GetAttribute("TotalItem");
    string totalprice = order.GetAttribute("TotalPrice");
    
    
    foreach (XmlElement orderLine in xmlDoc.SelectNodes("//OrderLine"))
    {
      string itemid = orderLine.GetAttribute("ItemId");
      string quantity = orderLine.GetAttribute("Quantity");
      string discountvalue = orderLine.GetAttribute("DiscountValue");
      string taxvalue = orderLine.GetAttribute("TaxValue");
      string price = orderLine.GetAttribute("Price");
    
      // need to store and/or output values here
    }
    
    


     

    Martin Honnen --- MVP XML
    My blog
  • Re: how to convert String to XML and read this XML???

    07-06-2009, 9:49 PM
    • Member
      18 point Member
    • vickyngo
    • Member since 06-10-2009, 5:31 AM
    • Vietnam
    • Posts 49

    Thanks to everyone for your input, it meaningful for me.  Martin_Honnen's answer was the closest for my specific implementation.

    sincerely,

    Vicky

    Vicky

    Please mark this as "Mark as Answer" if it helps you
Page 1 of 1 (13 items)
Microsoft Communities