ARTICLE

How to Read / Search Node or Attribute in XML file

Posted by Gohil Jayendrasinh Articles | XML December 21, 2010
In this article you will learn how to Read / Search Node or Attribute in XML file.
Reader Level:

My XML file is As Below:

<?xml version="1.0" encoding="UTF-8" standalone="no"?>

<Fields>

  <Field ID="1" Datatype="Option" Enabled="1" FieldClass="Normal" Name="Document Type" CaptionML="ENU=Document Type" OptionString="Quote,Order,Invoice,Credit Memo,Blanket Order,Return Order" OptionCaptionML="ENU=Quote,Order,Invoice,Credit Memo,Blanket Order,Return Order" BlankNumbers="DontBlank" BlankZero="0" SignDisplacement="0" Editable="1" NotBlank="0" Numeric="0" DateFormula="0" ClosingDates="0" Title="0" ValidateTableRelation="1" TestTableRelation="1" ExtendedDatatype="None"/>

  <Field ID="2" Datatype="Code" DataLength="20" Enabled="1" FieldClass="Normal" Name="Sell-to Customer No." CaptionML="ENU=Sell-to Customer No." BlankNumbers="DontBlank" BlankZero="0" SignDisplacement="0" Editable="0" NotBlank="0" Numeric="0" DateFormula="0" ClosingDates="0" Title="0" ValidateTableRelation="1" TestTableRelation="1" ExtendedDatatype="None">

    <TableRelations TableID="18" TableName="Customer" FieldID="0"/>

  </Field>

  <Field ID="3" Datatype="Code" DataLength="20" Enabled="1" FieldClass="Normal" Name="Document No." CaptionML="ENU=Document No." BlankNumbers="DontBlank" BlankZero="0" SignDisplacement="0" Editable="1" NotBlank="0" Numeric="0" DateFormula="0" ClosingDates="0" Title="0" ValidateTableRelation="1" TestTableRelation="1" ExtendedDatatype="None">

    <TableRelations TableID="36" TableName="Sales Header" FieldID="3">

      <Filters FilterGroup="0" FieldID="1" FilterType="FIELD" FilterValue="1"/>

    </TableRelations>

  </Field>

  <Field ID="4" Datatype="Integer" Enabled="1" FieldClass="Normal" Name="Line No." CaptionML="ENU=Line No." BlankNumbers="DontBlank" BlankZero="0" SignDisplacement="0" Editable="1" NotBlank="0" Numeric="0" DateFormula="0" ClosingDates="0" Title="0" ValidateTableRelation="1" TestTableRelation="1" ExtendedDatatype="None"/>

  <Field ID="5" Datatype="Option" Enabled="1" FieldClass="Normal" Name="Type" CaptionML="ENU=Type" OptionString=" ,G/L Account,Item,Resource,Fixed Asset,Charge (Item)" OptionCaptionML="ENU=&quot; ,G/L Account,Item,Resource,Fixed Asset,Charge (Item)&quot;" BlankNumbers="DontBlank" BlankZero="0" SignDisplacement="0" Editable="1" NotBlank="0" Numeric="0" DateFormula="0" ClosingDates="0" Title="0" ValidateTableRelation="1" TestTableRelation="1" OnValidate="134" ExtendedDatatype="None"/>

  <Field ID="6" Datatype="Code" DataLength="20" Enabled="1" FieldClass="Normal" Name="No." CaptionML="ENU=No." BlankNumbers="DontBlank" BlankZero="0" SignDisplacement="0" Editable="1" NotBlank="0" Numeric="0" DateFormula="0" ClosingDates="0" Title="0" ValidateTableRelation="1" TestTableRelation="1" OnValidate="135" ExtendedDatatype="None">
    <TableRelations TableID="7" TableName="Standard Text" FieldID="0">

      <Conditions FieldID="5" ConditionType="CONST" ConditionValue="0"/>

    </TableRelations>

    <TableRelations TableID="15" TableName="G/L Account" FieldID="0">

      <Conditions FieldID="5" ConditionType="CONST" ConditionValue="1"/>

    </TableRelations>

    <TableRelations TableID="27" TableName="Item" FieldID="0">

      <Conditions FieldID="5" ConditionType="CONST" ConditionValue="2"/>

    </TableRelations>

    <TableRelations TableID="156" TableName="Resource" FieldID="0">

      <Conditions FieldID="5" ConditionType="CONST" ConditionValue="3"/>

    </TableRelations>

    <TableRelations TableID="5600" TableName="Fixed Asset" FieldID="0">

      <Conditions FieldID="5" ConditionType="CONST" ConditionValue="4"/>

    </TableRelations>

    <TableRelations TableID="5800" TableName="Item Charge" FieldID="0">

      <Conditions FieldID="5" ConditionType="CONST" ConditionValue="5"/>

    </TableRelations>

  </Field>

  <Field ID="5800" Datatype="Boolean" Enabled="1" FieldClass="Normal" Name="Allow Item Charge Assignment" CaptionML="ENU=Allow Item Charge Assignment" BlankNumbers="DontBlank" BlankZero="0" SignDisplacement="0" Editable="1" InitValue="1" NotBlank="0" Numeric="0" DateFormula="0" ClosingDates="0" Title="0" ValidateTableRelation="1" TestTableRelation="1" OnValidate="206" ExtendedDatatype="None"/>

  <Field ID="5801" Datatype="Decimal" Enabled="1" FieldClass="FlowField" Name="Qty. to Assign" CaptionML="ENU=Qty. to Assign" BlankNumbers="DontBlank" BlankZero="0" SignDisplacement="0" Editable="0" NotBlank="0" Numeric="0" DateFormula="0" ClosingDates="0" Title="0" ValidateTableRelation="1" TestTableRelation="1" ExtendedDatatype="None">

    <CalculationFormula FlowType="Sum" ReverseSign="0" Table="5809" Field="8">

      <TableFilters>

        <Filter FilterGroup="0" FieldID="1" FilterType="FIELD" ValueIsFilter="0" OnlyMaxLimit="0" FilterValue="1"/>

        <Filter FilterGroup="0" FieldID="2" FilterType="FIELD" ValueIsFilter="0" OnlyMaxLimit="0" FilterValue="3"/>

        <Filter FilterGroup="0" FieldID="3" FilterType="FIELD" ValueIsFilter="0" OnlyMaxLimit="0" FilterValue="4"/>

      </TableFilters>

    </CalculationFormula>

  </Field>

  <Field ID="5802" Datatype="Decimal" Enabled="1" FieldClass="FlowField" Name="Qty. Assigned" CaptionML="ENU=Qty. Assigned" BlankNumbers="DontBlank" BlankZero="0" SignDisplacement="0" Editable="0" NotBlank="0" Numeric="0" DateFormula="0" ClosingDates="0" Title="0" ValidateTableRelation="1" TestTableRelation="1" ExtendedDatatype="None">

    <CalculationFormula FlowType="Sum" ReverseSign="0" Table="5809" Field="9">

      <TableFilters>

        <Filter FilterGroup="0" FieldID="1" FilterType="FIELD" ValueIsFilter="0" OnlyMaxLimit="0" FilterValue="1"/>

        <Filter FilterGroup="0" FieldID="2" FilterType="FIELD" ValueIsFilter="0" OnlyMaxLimit="0" FilterValue="3"/>

        <Filter FilterGroup="0" FieldID="3" FilterType="FIELD" ValueIsFilter="0" OnlyMaxLimit="0" FilterValue="4"/>

      </TableFilters>

    </CalculationFormula>

  </Field>

</Fields>

 

 Searching Code

 /// <summary>

        ///

        /// </summary>

        /// <param name="XMLPath">File Path</param>

        /// <param name="SearchNode">Search Node</param>

        /// <param name="AttributeName">Attribute Node</param>

        /// <param name="AttributeValue">Attribute Value</param>

        /// <param name="Connectionstring">Database ConnectionString</param>

        private void InsertintoTableRelationInfo(string XMLPath, string SearchNode, string AttributeName, string AttributeValue, string Connectionstring)

        {

            #region Table RelationInfo

            XmlDocument doc = new XmlDocument();

            doc.Load(XMLPath);

 

            System.Text.StringBuilder str = new StringBuilder();

 

            XmlNodeList nodes = doc.GetElementsByTagName(SearchNode);

 

            foreach (XmlNode node in nodes)

            {

 

                if (node.HasChildNodes)

                {

                    foreach (XmlAttribute attribute in node.Attributes)

                    {

                        if (attribute.Name == AttributeName)

                        {

                            if (attribute.Value == AttributeValue)

                            {

 

                                for (int i = 0; i < node.ChildNodes.Count; i++)

                                {

                                    string name = node.ChildNodes[i].Name;

                                    /*

                                     * Check ChildNode

                                     */

                                    if (name == "TableRelations")

                                    {

                                        foreach (XmlAttribute chieldAttribute in node.ChildNodes[i].Attributes)

                                        {

                                            /*

                                             * Check ChildNode Attribute

                                             */

                                            if (chieldAttribute.Name == "TableID")

                                            {

                                                _TableRelationsTableID = Convert.ToInt32(chieldAttribute.Value);

 

                                            }

                                            if (chieldAttribute.Name == "TableName")

                                            {

                                                _TableName = chieldAttribute.Value;

                                            }

                                            if (chieldAttribute.Name == "FieldID")

                                            {

                                                _TableFieldID = Convert.ToInt32(chieldAttribute.Value);

                                            }                                           

                                        }

                                        _CalculationFormulaFlowType = "TableRelation";

                                        using (dbNavXmlDataContext db = new dbNavXmlDataContext(Connectionstring))

                                        {

                                            db.USP_INSERT_TableRelationInfo(_ObjectType, _ObjectID, _ID, Convert.ToInt32(AttributeValue), _TableRelationsTableID, _TableName, _TableFieldID, _CalculationFormulaFlowType, _ReverseSign);

                                        }

                                    }

                                    if (name == "CalculationFormula")

                                    {

                                        foreach (XmlAttribute chieldAttribute in node.ChildNodes[i].Attributes)

                                        {

                                            if (chieldAttribute.Name == "FlowType")

                                            {

                                                _CalculationFormulaFlowType = chieldAttribute.Value;

 

                                            }

                                            if (chieldAttribute.Name == "ReverseSign")

                                            {

                                                _ReverseSign = Convert.ToInt32(chieldAttribute.Value);

                                            }

                                          

                                        }

                                        

                                        using (dbNavXmlDataContext db = new dbNavXmlDataContext(Connectionstring))

                                        {

                                            db.USP_INSERT_TableRelationInfo(_ObjectType, _ObjectID, _ID, Convert.ToInt32(AttributeValue), _TableRelationsTableID, _TableName, _TableFieldID, _CalculationFormulaFlowType, _ReverseSign);

                                        }

                                    }

                                }

                            }
                       }

                    }

                 }
 
            #endregion
            }
 
        }

Login to add your contents and source code to this article
post comment
     

I am a flash developer and I found it to be so easy processing/importing XML data in AS3.0. With C# on the other hand I found it to be more like dancing with an enraged bull. So i decided to create a class file that I can import into my projects where I can just pass the variables. I looked over your article and tried applying it to reading an element attribute value and ran into a snag. Source Code below... <br /><br /> //Global variables within class <br /><br /> String file_reference;<br /> XmlDocument xmlDoc;<br /> XmlElement xmlElem;<br /> XmlNodeList recordSet;<br /> <br /> //XML Attribute read function<br /> <br /> public String xml_element_attribute_value(String elementName, int index, String attributeName)<br /> {<br /> if (index < recordSet.Count)<br /> {<br /> if (recordSet[index].Name.ToString() == elementName)<br /> if (recordSet[index].Attributes.Name.ToString() == attributeName) // Error:: Does not contain a definition for "Name"<br /> return recordSet[index].Attributes.Value; // Error:: Doa not contain a definition for "Value"<br /> }<br /> }<br /> <br /><br /> XML Data is loaded via constructor function. All I am trying to do is simplifying the entire process so I can make my XML data sources more relationally formatted instead of using nested values within values. As it stands I can use element attributes as every attempt I made to use them has failed. Can you help me with this?

Posted by Slam Dunk Dec 23, 2010
COMMENT USING
PREMIUM SPONSORS
Over-C is a holistic consortium of communications and technology specialists. We build, deploy and market both business as well as consumer products and solutions.
Get Career Advice from Experts
SPONSORED BY
  • PDF reports have never been easier to create. With our included WYSIWYG Designer, you can layout your reports, set up your data source and let DynamicPDF ReportWriter do the rest.
Get Career Advice from Experts