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:
<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:
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