0

I have an XML file from which i have to extract only the attribute values in the tag. The XML kind of looks in this structure

<customer> 
    <customerMiddleInitial>W</customerMiddleInitial> 
<name>
      <FirstName>XXXXXXXX</FirstName>
      <LastName> YYYYYYYY</LastName>
</name>
    <customerBirth>1983-01-01</customerBirth> 
    <customerWorkPhone>020 1234567</customerWorkPhone> 
    <customerMobilePhone>0799 1234567</customerMobilePhone> 
    <previousCust>0</previousCust> 
    <timeOnFile>10</timeOnFile> 
    <customerId>CUST123</customerId>
</customer>

So, I want to extract all the details between the tags. The expected output should be all the customer details.

How can i implement this in C#? Any help will be appreciated.

Goutham Ramesh
  • 103
  • 1
  • 2
  • 13
  • How to parse XML in C#: https://msdn.microsoft.com/fr-fr/library/cc189056(v=vs.95).aspx – DanielY Mar 23 '17 at 05:35
  • http://stackoverflow.com/questions/364253/how-to-deserialize-xml-document I think you will find your answer in the link above. – Hari Govind Mar 23 '17 at 06:17
  • Without any attempted code which you have troubled with to start, this is simply a duplicate of many older 'how to parse XML in C#' kinda questions... – har07 Mar 23 '17 at 06:45

2 Answers2

0
XmlDocument DOC = new XmlDocument();
DOC.Load("LoadYourXMLHere.xml");
XmlNodeList ParentNode = DOC.GetElementsByTagName("customer");
foreach (XmlNode AllNodes in ParentNode)
{
 if (ParentNode == DOC.GetElementsByTagName("customerMiddleInitial"))
{
    customer.Initial = AllNodes["customerMiddleInitial"].InnerText;
}
if (ParentNode == DOC.GetElementsByTagName("name"))
{
    customer.FirstName= AllNodes["FirstName"].InnerText;
    customer.LastName= AllNodes["LastName"].InnerText;
}
if (ParentNode == DOC.GetElementsByTagName("customerBirth"))
{
    customer.Birthdate= AllNodes["customerBirth"].InnerText;
}
if (ParentNode == DOC.GetElementsByTagName("customerWorkPhone"))
{
    customer.WorkPhone= AllNodes["customerWorkPhone"].InnerText;
}
if (ParentNode == DOC.GetElementsByTagName("customerMobilePhone"))
{
    customer.MobilePhone = AllNodes["customerMobilePhone"].InnerText;
}
if (ParentNode == DOC.GetElementsByTagName("previousCust"))
{
    customer.PreviousCust= AllNodes["previousCust"].InnerText;
}
if (ParentNode == DOC.GetElementsByTagName("timeOnFile"))
{
    customer.TimeOnFile= AllNodes["timeOnFile"].InnerText;
}
if (ParentNode == DOC.GetElementsByTagName("customerId"))
{
    customer.ID= AllNodes["customerId"].InnerText;
}
} 

Create a Customer model and execute the above xml parsing in C#.

Sincerely,

Thiyagu Rajendran

**Please mark the replies as answers if they help and unmark if they don't.

0

First of all you need to see this answer

When you generated model just use this code for deserialised xml to object.And after that just use model to work with your data. That is easy and simple

    public static T FromXml<T>(String xml)
    {
        T returnedXmlClass = default(T);

        try
        {
            using (TextReader reader = new StringReader(xml))
            {
                try
                {
                    returnedXmlClass =
                            (T)new XmlSerializer(typeof(T)).Deserialize(reader);
                }
                catch (InvalidOperationException)
                {
                    // String passed is not XML, simply return defaultXmlClass
                }
            }
        }
        catch (Exception ex)
        {
        }

        return returnedXmlClass;
    }

and using

var model = FromXml<customer>(yourXmlString);
Community
  • 1
  • 1
Artsiom Che
  • 122
  • 8