Click here to Skip to main content
15,881,651 members
Please Sign up or sign in to vote.
4.20/5 (2 votes)
See more:
Hi All,

I am new to xml in .Net Development,

will you please tell me the method through which i can able to parse xml string into key value pair.

my xml string is like given below

XML
<position>
   <id>56565</id>
   <title>Analyst</title>
   <summary>Application Programming,Database Programming,Application Support and Maintenance</summary>
   <start-date>
      <year>2013</year>
      <month>7</month>
   </start-date>
   <is-current>true</is-current>
   <company>
      <id>434</id>
      <name>xyz limited</name>
   </company>
</position>



thanks in advance
Posted
Comments
_Asif_ 27-Aug-14 7:35am    
What do you mean by Key value pair?

Use an XmlDocument[^] and call the LoadXml[^] method.
 
Share this answer
 
This will help you convert XML to Object
XML Serialization and Deserialization: Part-1[^]
 
Share this answer
 
You Can Try this
I have Considered the following Source XML


HTML
<root>
  <position>
     <id> 1 </id>
     <title> ABC <title>
     <company> PQR <company>
  </company></company></title></title></position>
</root>


And Target XML as
HTML
<root>
</root>


Code Goes Here

C#
XmlDocument xDoc =  new XmlDocument();
XmlDocument xDoc2 =  new XmlDocument();

xDoc.Load(HttpContext.Current.Server.MapPath("menu.xml"); // source xml
xDoc2.Load(HttpContext.Current.Server.MapPath("menu1.xml"); // target xml 

XmlNode Page = null;
string[] str = {"id","title","company"} //Your attributes with your logic
XmlNode Data = xDoc2.CreateNode(XmlNodeType.Element,"position",null);

for(int i = 0 ; i < 3 ; i++)
{
   Page = xDoc.SelectSingleNode("position/" + str[i]);
   XmlAttribute item = xDoc2.CreateAttribute(str[i]);
   item.Value = Page.InnerText;
   Data.Attributes.Append(item);
   xDoc2.DocumentElement.AppendChild(Data);

}

xDoc2.Save(HttpContext.Current.Server.MapPath("menu1.xml");



Your OutPut will be
HTML
<root>
  <position id=1 title=ABC company=PQR />
</root>
 
Share this answer
 
v2
Almost everything you need is here
http://msdn.microsoft.com/en-us/library/system.xml.linq.xdocument%28v=vs.110%29.aspx[^]


Also noteworthy will be learning about XPathExpressions which would allow you to read and select particular nodes/series of nodes in XML as per your choice.

Would definitely recommend following to get basics of XPath.
http://www.w3schools.com/XPath/[^]
 
Share this answer
 

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