Click here to Skip to main content
15,880,608 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi All,
I have an XML as follows.

XML
<?xml version="1.0" encoding="utf-8"?>
<cpeif:CustomerProfileResponse profileName="CustomerInformation" version="100" 
		 xmlns:cpeif=""	xmlns:ppns="" xmlns:tpns="">
        <cpeif:Person>		
		<ppns:CustID>1</ppns:CustID>
		<ppns:CPrefix>CPrefix3</ppns:CPrefix>
		<ppns:CFName>CFName3</ppns:CFName>
		<ppns:CMName >CMName3</ppns:CMName>
		<ppns:CLName>CLName3</ppns:CLName>
		</cpeif:Person>	
	<cpeif:TransactionDetail>		
		<tpns:CHHID>0</tpns:CHHID>
		<tpns:Transactions>
			<tpns:Transaction>
				<tpns:StoreID>0</tpns:StoreID>
				<tpns:CustID>1</tpns:CustID>
				<tpns:Item>Item1</tpns:Item>
			</tpns:Transaction>
			<tpns:Transaction>
				<tpns:StoreID>4294967295</tpns:StoreID>
				<tpns:CustID>1</tpns:CustID>
				<tpns:Item>Item2</tpns:Item>
			</tpns:Transaction>
			<tpns:Transaction>
				<tpns:StoreID>1</tpns:StoreID>
				<tpns:CustID>1</tpns:CustID>
				<tpns:Item>Item3</tpns:Item>
			</tpns:Transaction>
		</tpns:Transactions>
	</cpeif:TransactionDetail>	
	</cpeif:CustomerProfileResponse>


I need to make it as the following

XML
<?xml version="1.0" encoding="utf-8"?>
<cpeif:CustomerProfileResponse profileName="CustomerInformation" version="100"
		 xmlns:cpeif="" xmlns:ppns="" xmlns:tpns=""
		xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
	<cpeif:Customer>
		<ppns:CustID>1</ppns:CustID>
		<ppns:CPrefix>CPrefix3</ppns:CPrefix>
		<ppns:CFName>CFName3</ppns:CFName>
		<ppns:CMName >CMName3</ppns:CMName>
		<ppns:CLName>CLName3</ppns:CLName>	
		<tpns:StoreID>1</tpns:StoreID>
		<tpns:CustID>1</tpns:CustID>
		<tpns:SOrderNo>0</tpns:SOrderNo>
		<tpns:Date>0</tpns:Date>
		<tpns:Item>Item1</tpns:Item>		
	</cpeif:Customer>
	<cpeif:Customer>
		<ppns:CustID>1</ppns:CustID>
		<ppns:CPrefix>CPrefix3</ppns:CPrefix>
		<ppns:CFName>CFName3</ppns:CFName>
		<ppns:CMName >CMName3</ppns:CMName>
		<ppns:CLName>CLName3</ppns:CLName>
		<tpns:StoreID>4294967295</tpns:StoreID>
		<tpns:CustID>1</tpns:CustID>
		<tpns:SOrderNo>4294967295</tpns:SOrderNo>
		<tpns:Date>4294967295</tpns:Date>
		<tpns:Item>Item2</tpns:Item>
	</cpeif:Customer>
	<cpeif:Customer>
		<ppns:CustID>1</ppns:CustID>
		<ppns:CPrefix>CPrefix3</ppns:CPrefix>
		<ppns:CFName>CFName3</ppns:CFName>
		<ppns:CMName >CMName3</ppns:CMName>
		<ppns:CLName>CLName3</ppns:CLName>
		<tpns:StoreID>1</tpns:StoreID>
		<tpns:CustID>1</tpns:CustID>
		<tpns:SOrderNo>2</tpns:SOrderNo>
		<tpns:Date>1</tpns:Date>
		<tpns:Item>Item3</tpns:Item>
	</cpeif:Customer>
	</cpeif:CustomerProfileResponse>

Here customer is doing 3 different transaction, so i need to show as three different tabs.Can any one help me doing this. Any help is appreciated. Thanks in advance.

I have found a similar solution here :http://stackoverflow.com/questions/9215273/flattening-an-xml-document but that is not working out in my case.
Kindest Regards
Sibeesh
Posted
Updated 19-Nov-14 6:38am
v2
Comments
Maciej Los 19-Nov-14 12:14pm    
What have you done? Where are you stuck?
[no name] 19-Nov-14 12:24pm    
I have tried so many things , but still I am not getting an apt way for this.
[no name] 19-Nov-14 12:25pm    
private static void flatterningXML()
{
try
{
var doc = XDocument.Load("test.xml");
XNamespace ns = "mynamespace";
var member = doc.Root.Element(ns + "CustomerProfileResponse");

// This will *sort* of flatten, but create copies...
var descendants = member.Descendants().ToList();

// So we need to strip child elements from everywhere...
// (but only elements, not text nodes). The ToList() call
// materializes the query, so we're not removing while we're iterating.
foreach (var nested in descendants.Elements().ToList())
{
nested.Remove();
}
member.ReplaceNodes(descendants);
}
catch
{
}
}
I have tried this.

Use XML parsing and generated facilities offered by .NET FCL. This is my short overview of them:

  1. Use System.Xml.XmlDocument class. It implements DOM interface; this way is the easiest and good enough if the size if the document is not too big.
    See http://msdn.microsoft.com/en-us/library/system.xml.xmldocument.aspx[^].
  2. Use the classes System.Xml.XmlTextWriter and System.Xml.XmlTextReader; this is the fastest way of reading, especially is you need to skip some data.
    See http://msdn.microsoft.com/en-us/library/system.xml.xmlwriter.aspx[^], http://msdn.microsoft.com/en-us/library/system.xml.xmlreader.aspx[^].
  3. Use the class System.Xml.Linq.XDocument; this is the most adequate way similar to that of XmlDocument, supporting LINQ to XML Programming.
    See http://msdn.microsoft.com/en-us/library/system.xml.xmldocument.aspx[^], http://msdn.microsoft.com/en-us/library/bb387063.aspx[^].


Good luck.
—SA
 
Share this answer
 
You could use XSLT also. If you can define the transformation specification, it is quite simple to apply concrete transformation from code. See: http://msdn.microsoft.com/en-us/library/system.xml.xsl.xslcompiledtransform(v=vs.110).aspx[^]
 
Share this answer
 
Comments
Maciej Los 19-Nov-14 14:20pm    
Follow the link posted by OP in his question.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900