reading xml tags attributes

asmgx

This is my XML file

<?xml version="1.0" encoding="UTF-8"?>
<xmi:XMI xmlns:util="http://" xmlns:tcas="http://" xmlns:xmi="http://www.omg.org/XMI" xmlns:cas="http:/" xmlns:type10="http:///" xmlns:ne="http:///" xmlns:group3="http:///org/"  xmi:version="2.0">
    <cas:NULL xmi:id="0"/>
    <group1:Black xmi:id="19" code="1" ref="0" mask="23086" id="SIMPLE_Black"/>
    <group1:White xmi:id="25" code="2" ref="0" mask="21" WhiteNumber="0"/>
    <group1:White xmi:id="31" code="7" ref="23" mask="39" WhiteNumber="1"/>
    <group1:White xmi:id="37" code="3" ref="53" mask="68" WhiteNumber="2"/>
    <group1:White xmi:id="43" code="7" ref="71" mask="86" WhiteNumber="3"/>
    <group1:White xmi:id="49" code="3" ref="88" mask="102" WhiteNumber="4"/>
    <group2:Pink xmi:id="8745" code="4" ref="20613" mask="20614" other="5904"/>
    <group2:Pink xmi:id="8753" code="7" ref="20624" mask="20625" other="5907"/>
    <group2:Pink xmi:id="8761" code="5" ref="20625" mask="20626" other="5908"/>
    <group2:Pink xmi:id="8769" code="2" ref="20640" mask="20641" other="5911"/>
    <group2:Pink xmi:id="8777" code="1" ref="20641" mask="20642" other="5912"/>
    <group2:Pink xmi:id="8785" code="6" ref="20701" mask="20702" other="5923"/>
    <group2:Blue xmi:id="31715" code="3" ref="6959" mask="6966" other="2457" />
    <group2:Blue xmi:id="31727" code="5" ref="6967" mask="6971" other="2458" />
    <group2:Blue xmi:id="31747" code="7" ref="6973" mask="6977" other="2460" />
    <group2:Blue xmi:id="31759" code="2" ref="6978" mask="6981" other="2461" />
    <group2:Blue xmi:id="31771" code="8" ref="6982" mask="6991" other="2463" />
    <group2:Blue xmi:id="31783" code="8" ref="6992" mask="6993" other="2464" />
    <group2:Blue xmi:id="31795" code="8" ref="6994" mask="7002" other="2465" />
    <group2:Blue xmi:id="31807" code="9" ref="7003" mask="7013" other="2466" />
    <group2:Blue xmi:id="31827" code="3" ref="7015" mask="7022" other="2468" />
    <group2:Blue xmi:id="31847" code="1" ref="7024" mask="7026" other="2470" />
    <group2:Red xmi:id="29184" code="2" ref="6100" mask="6101" other="2178" />
    <group2:Red xmi:id="29217" code="1" ref="6105" mask="6106" other="2182" />
    <group2:Red xmi:id="29234" code="4" ref="6109" mask="6110" other="2184" />
    <group2:Red xmi:id="29278" code="1" ref="6128" mask="6129" other="2188" />
    <group2:Yellow xmi:id="30300" code="4" ref="6398" mask="6400" other="2304" />
    <group2:Yellow xmi:id="30333" code="1" ref="6404" mask="6406" other="2308" />
    <group2:Yellow xmi:id="30394" code="5" ref="6426" mask="6429" other="2314" />
    <group2:Yellow xmi:id="30431" code="1" ref="6437" mask="6439" other="2318" />
    <group2:Yellow xmi:id="30468" code="6" ref="6447" mask="6450" other="2322" />
    <group2:Green xmi:id="79301" code="1" ref="2501" mask="2505" GreenType="NP"/>
    <group2:Green xmi:id="79306" code="6" ref="2505" mask="2506" GreenType="O"/>
    <group2:Green xmi:id="79311" code="1" ref="2507" mask="2520" GreenType="ADJP"/>
    <group2:Green xmi:id="79316" code="1" ref="2521" mask="2523" GreenType="PP"/>
    <group2:Green xmi:id="79321" code="1" ref="2524" mask="2542" GreenType="NP"/>
    <group3:Brown xmi:id="117792" code="7" ref="16421" mask="16426" id="0" max="0"/>
    <group3:Brown xmi:id="119483" code="1" ref="16486" mask="16497" id="0" />
    <group3:Brown xmi:id="117469" code="1" ref="16486" mask="16492" id="0" />
    <group3:Brown xmi:id="119364" code="8" ref="16493" mask="16497" id="0" />
    <group2:Grey xmi:id="137117" code="1" ref="143" mask="150" id="1" />
    <group2:Grey xmi:id="137131" code="1" ref="150" mask="151" id="2" />
    <group2:Grey xmi:id="137145" code="8" ref="152" mask="159" id="0"/>
    <group2:Grey xmi:id="137159" code="1" ref="152" mask="159" id="1" />
    <group3:Purple xmi:id="236545" id="0" category="R" argument="236523"/>
    <group3:Purple xmi:id="235624" id="0" category="A" argument="235612"/>
    <group3:Purple xmi:id="232638" id="0" category="A" argument="232632"/>
    <group3:Purple xmi:id="236845" id="0" category="A" argument="236821"/>
    <group3:Purple xmi:id="242015" id="0" category="C" argument="242003"/>
</xmi:XMI>

It is a list of colours and each colour has some attributes

I want to get few colors (White, Blue, Brown, Yellow)

and read its attributes and put it in a database

I managed to read one tag only and could not get its attributes

This is my code so far

        XmlDocument xmldoc = new XmlDocument();
        XmlNodeList xmlnode;
        int i = 0;
        string str = null;
        FileStream fs = new FileStream(@"c:\Output\1.xmi", FileMode.Open, FileAccess.Read);
        xmldoc.Load(fs);
        xmlnode = xmldoc.GetElementsByTagName("group1:White");
        for (i = 0; i <= xmlnode.Count - 1; i++)
        {
            str = xmlnode[i].Item("code").InnerText.Trim() + "  " + xmlnode[i].Item("ref").InnerText.Trim() + "  " + xmlnode[i].Item("mask").InnerText.Trim();
            MessageBox.Show(str);
        }

I got xmlnode[i] correct but no attributes !

how to read tags attributes?

Mohammed Sajid

Note that, group1 and group2 are not declared in the posted Xml.

For the question how to read tags attributes?, you could access to attribute, by calling .Attributes["AttributeName"], like :

....
xmlnode = xmldoc.GetElementsByTagName("group1:White");
for (i = 0; i <= xmlnode.Count - 1; i++)
{
    str = xmlnode[i].Attributes["code"].Value + "  " + xmlnode[i].Attributes["ref"].Value + "  " + xmlnode[i].Attributes["mask"].Value;
    MessageBox.Show(str);
}
....

I hope you find this helpful.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Reading XML that uses attributes

XML Reading with Attributes in Java

Reading an xml with nested tags

How to access attributes of XML tags

Reading tags in an XML CLOB column

SQL Oracle XML empty tags and attributes

Parsing xml with same tags and different attributes

Extracting attributes that are in XML tags with BeautifulSoup4

Objectify xml string with dashes in tags and attributes names

COBOL XML GENERATE using tags and attributes

nested tags and attributes in BeautifulSOUP and OpenStreetMap XML

Remove empty XML tags with PHP but ignore tags with attributes

Reading XML repeated tags in sql server

C# - XML reading outside of tags/elements

Reading long text in between tags in XML

Reading tags from xml file into php array

How can I check the existence of attributes and tags in XML before parsing?

How can I check the existence of attributes and tags in XML before parsing?

Parsing XML with 'nil' attributes and empty 'element' tags using 'xmlbf'

Search and replace in only certain tags and attributes in XML file by a regular expression

How to read attributes of xml tags in groovy Jenkins pipeline

C# Linq to XML read multiple tags with attributes

Python Beautiful Soup XML Conditional Query (mixed tags and attributes) Parsing

Merge two XML files and add missing tags and attributes

XSLT to access and create nested xml and convert tags to attributes

Marshalling a bean object to XML tags having same attributes through JAXB

R: Read XML file and delete tags with specific attributes

How to use XML attributes, instead of element tags to build a database with XSLT

Filtering XML tags with attributes using Where-Object