Click here to Skip to main content
15,881,559 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am trying to read out an XML file but while reading the child node I am facing difficulty. In the below attached XML file kindly suggest me how to read the child node "UoM" and "Quantity" of Density?

I tried using the foreach loop just as the items for density but that didn't worked out for me. Kindly help me out with this.

Here is the XML file :


XML
<itemtomes>
  <applicationarea>
    <messagetype>ADD/UPATE ITEM
    <description>Items from ERP to MES
    <sender>M3
    <receiver>WONDERWARE
    <creationdatetime>2019-11-09 12:45:10.978
    <confirmation>Never
    <errormsg>
  
  <dataarea>
    <items>
      <item>
        <itemcode>10002912
        <itemname>L-Valine 96.5%
        <itemtype>152 Feed Additives Small packaging
        <status>P
        <shelflife>365
        <site>75A
        <uom>KG
        <packagetype>SMALLBAG
        <packagesize>25
        <density>
          <quantity>0.9
          <uom>L/KG
        
        <alternativematerials>
          <alternativematerial>
            <itemcode>10002911
          
        
      
      <item>
        <itemcode>10006351
        <itemname>Wheat Flour Fine
        <itemtype>101 Feed Materials Large packaging
        <status>A
        <shelflife>365
        <site>75A
        <uom>KG
        <packagetype>BIGBAG
        <packagesize>1000
        <density>
          <quantity>1.2
          <uom>L/KG
        
        <alternativematerials>
          <alternativematerial>
            <itemcode>

I have tried the below code but was facing difficulty :

C#
{================================================================================================================+=}

DT = Now();
SQL_Connection = new System.Data.SqlClient.SqlConnection("Server=localhost;Database=NUTRECOM3;User ID=sa;Password=ssmits@1234");


FOR EACH XML_FILES in System.IO.Directory.GetFiles(Me.FilePath,"*Item*.xml");
    LogMessage("XML_FILES::"+XML_FILES);


        Doc = new System.Xml.XmlDocument;
        Doc.Load(XML_FILES);

        XMLTBL = doc.NameTable;
        XML_NSM = new System.Xml.XmlNamespaceManager(XMLTBL);
        'XML_NSM.AddNamespace("asx","http://www.sap.com/abapxml");

{========================================================APPLICATION AREA=============================================}

MessageType = Doc.SelectSingleNode("/ItemToMES/ApplicationArea/MessageType").InnerText;
Description = Doc.SelectSingleNode("/ItemToMES/ApplicationArea/Description").InnerText;
Sender = Doc.SelectSingleNode("/ItemToMES/ApplicationArea/Sender").InnerText;
Receiver = Doc.SelectSingleNode("/ItemToMES/ApplicationArea/Receiver").InnerText;;
CreationDateTime = Doc.SelectSingleNode("/ItemToMES/ApplicationArea/CreationDateTime").InnerText;
Confirmation = Doc.SelectSingleNode("/ItemToMES/ApplicationArea/Confirmation").InnerText;

{======================================================================================================================}

dim m_nodelist as System.Xml.XmlNodeList;
dim m_node as System.Xml.XmlNode;
    m_nodelist = Doc.getElementsByTagName("Item");
    LogMessage(m_nodelist.Count);
    Z=1;
    X=1;



for each m_node in m_nodelist;


{================================================= READ - FIRST FIELD =================================================}

        ''SQL_FTPC_OBJ_TYPE =Doc.SelectSingleNode("/asx:abap/asx:values/TAB/item/FTPC_OBJ_TYPE",XML_NSM).InnerText;
        ''LogMessage(SQL_FTPC_OBJ_TYPE);

        ItemCode =m_node.Item("ItemCode").InnerText;

    if (ItemCode == null and ItemCode == "") then
        LogMessage("ItemCode is not having any value");
    else
        LogMessage("ItemCode = " + ItemCode);
    endif;


What I have tried:

Tried using for each on Density node but that didn't worked out for me
Posted
Updated 3-Dec-19 21:39pm
v2
Comments
phil.o 4-Dec-19 3:34am    
This is neither well-formed XML, nor proper c# code.
Maciej Los 10-Dec-19 15:22pm    
Short And To The Point!
RickZeeland 4-Dec-19 4:57am    
Do you want the VB.NET code to work with the malformed XML, or can you get a proper XML file?
Richard Deeming 4-Dec-19 10:10am    
Your application should not be connecting to SQL as sa. That's an unrestricted user account which could be used to destroy your database, your server, or even your network.

And I hope that's not your real sa password you've just posted to a public forum?! If it is, you should change it immediately. And this time, chose a strong password, rather than something that could be cracked in a few minutes.

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