Click here to Skip to main content
15,070,139 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,
I'm trying to read attributes from an xml file with C#.,
-but the code I use skips rows & reads only every other attribute with the same element name.

- Does anyone know better than me how to read all the attributes of 'cfgattr' with C#?

** XML structure & content is made by another, so I need to be able to read all attributes without having to change xml structure.

'simple' & 'Condition' attributes works fine to read this way, it is when elements are repeated as for 'cfgattr' that only every other line works ..

Thanks in advance!! // Michael

What I have tried:

The XML:
XML
<application
	xmlns="http://www.xxx.com/aaa/bbb/ccc" >
	<eventblock>
		<blockhdr ver="1.2" serverid="1" />
		<emsg evid="12345678901234567890" >
			<simple tag="simple_tag" etime="2021-07-13T15:20:11.3285" etype="3" ecat="Condition.Message" prty="1" eventdscr="TEST_EVENT"/>
			<tracking/>
			<condition ep="testsmr" scond="testsmr" chs="1" cha="0" che="0" newss="1" newsa="1" newse="1" areq="0" acttime="2021-07-13T15:20:11.3285"/>
			<cfgattr name="name1" type="xs:string" value="1"/>
			<cfgattr name="name2" type="xs:string" value="2"/>
			<cfgattr name="name3" type="xs:string" value="3"/>
			<cfgattr name="name4" type="xs:string" value="4"/>
			<cfgattr name="name5" type="xs:string" value="5"/>
			<cfgattr name="name6" type="xs:int" value="6"/>
			<cfgattr name="name7" type="xs:int" value="7"/>
		</emsg>
	</eventblock>
</application>

THE C# code to illustrate my issue:
C#
using System;
using System.IO;
using System.Xml;

namespace xml_test
{
    class Program
    {
        static void Main(string[] args)
        {
           XmlReaderSettings settings = new XmlReaderSettings
            {
                IgnoreWhitespace = true,
                ValidationType = ValidationType.None
            };

            using (var fileStream = File.OpenText(@"c:\txt\xml_file.xml"))
            using (XmlReader reader = XmlReader.Create(fileStream, settings))
            {
                while (reader.Read())
                {
                    bool cfg = reader.ReadToFollowing("cfgattr");
                    if (cfg==true)
                    {
                    string cfg_name = reader.GetAttribute("name");
                    string cfg_type = reader.GetAttribute("type");
                    string cfg_value = reader.GetAttribute("value");
                    Console.WriteLine(" Name:" + cfg_name + " Type " + cfg_type + " Value: " + cfg_value);
                    }
                }
            }
        }
    }
}



The Consol Output:
Name:name1 Type xs:string Value: 1
Name:name3 Type xs:string Value: 3
Name:name5 Type xs:string Value: 5
Name:name7 Type xs:int Value: 7
Posted
Updated 13-Jul-21 5:22am
v3
Comments
PIEBALDconsult 13-Jul-21 10:56am
   
Reading XML nodes which have namespaces -- particularly a default namespace -- is tricky. I'm surprised you're receiving anything at all.
Other than that, I must assume that it's doing exactly what you told it to do.
mh_user 13-Jul-21 11:05am
   
Yes.. I know that now ;p

I'd also tried to validate with schema from vs generated xsd-file, but it didn't help to solve to read every row..

I don't know, but maybe try

C#
bool cfg ;
while (cfg = reader.ReadToFollowing("cfgattr"))


instead?
   
Look at your loop:
C#
while (reader.Read())
 {
     bool cfg = reader.ReadToFollowing("cfgattr"); // doing this every time skips a line
     if (cfg==true)

You should only do the ReadToFollowing for the tag once at the beginning, that is before the while loop.
   
Thank YOU!!
You saved my day!!

This is the working code after your Suggested adjustment!

using System;
using System.IO;
using System.Xml;

namespace xml_test
{
    class Program
    {
        static void Main(string[] args)
        {
           XmlReaderSettings settings = new XmlReaderSettings
            {
                IgnoreWhitespace = true,
                ValidationType = ValidationType.None
            };

            using (var fileStream = File.OpenText(@"c:\txt\xml_file.xml"))
            using (XmlReader reader = XmlReader.Create(fileStream, settings))
            {
                bool cfg;
                while (cfg = reader.ReadToFollowing("cfgattr"))
                { 
                    string cfg_name = reader.GetAttribute("name");
                    string cfg_type = reader.GetAttribute("type");
                    string cfg_value = reader.GetAttribute("value");
                    Console.WriteLine(" Name:" + cfg_name + " Type " + cfg_type + " Value: " + cfg_value);
                }
            }
        }
    }
}
   

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