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!
I would love to change the world, but they wont give me the source code.
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.
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!
I would love to change the world, but they wont give me the source code.
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;
vickyngo
Member
26 Points
56 Posts
how to convert String to XML and read this XML???
Jul 06, 2009 02:01 AM|LINK
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
Please mark this as "Mark as Answer" if it helps you
RickNZ
Contributor
5233 Points
880 Posts
Re: how to convert String to XML ???
Jul 06, 2009 03:47 AM|LINK
Try this:
Ultra-Fast ASP.NET: Build Ultra-Fast and Ultra-Scalable web sites using ASP.NET and SQL Server
My blog: http://www.12knowmore.com
malcolms
All-Star
18687 Points
3124 Posts
MVP
Re: how to convert String to XML ???
Jul 06, 2009 04:41 AM|LINK
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);kavita_khand...
Star
9767 Points
1931 Posts
Re: how to convert String to XML ???
Jul 06, 2009 04:56 AM|LINK
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.
I would love to change the world, but they wont give me the source code.
vickyngo
Member
26 Points
56 Posts
Re: how to convert String to XML and read this XML???
Jul 06, 2009 05:24 AM|LINK
Thanks so much! I want to ask after convert string to xml then to read this xml how i do to read it?
Please mark this as "Mark as Answer" if it helps you
kavita_khand...
Star
9767 Points
1931 Posts
Re: how to convert String to XML and read this XML???
Jul 06, 2009 05:51 AM|LINK
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.
*
*/
I would love to change the world, but they wont give me the source code.
vickyngo
Member
26 Points
56 Posts
Re: how to convert String to XML and read this XML???
Jul 06, 2009 06:58 AM|LINK
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"
"
Please help me
Best regards,
Vicky
Please mark this as "Mark as Answer" if it helps you
kavita_khand...
Star
9767 Points
1931 Posts
Re: how to convert String to XML and read this XML???
Jul 06, 2009 08:04 AM|LINK
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)
I would love to change the world, but they wont give me the source code.
vickyngo
Member
26 Points
56 Posts
Re: how to convert String to XML and read this XML???
Jul 06, 2009 08:28 AM|LINK
Hic, my code is still wrong...
Please mark this as "Mark as Answer" if it helps you
kavita_khand...
Star
9767 Points
1931 Posts
Re: how to convert String to XML and read this XML???
Jul 06, 2009 08:48 AM|LINK
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?
I would love to change the world, but they wont give me the source code.