Click here to Skip to main content
15,867,895 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Guys it can read all elements but it can't read Attribute. This is link: https://www.korayspor.com/grisport.xml

I need read to attributes of Categories. It is "Kategori" in Turkish. Like "Kategori no="8988">

This code doesn't work:
C#
case XmlNodeType.Attribute: 
                    xmlSet.WriteAttributeString(reader.Name, reader.Value);
                    break;


C#
XmlTextReader reader = new XmlTextReader("https://www.korayspor.com/grisport.xml");
        XmlTextWriter xmlSet = new XmlTextWriter("sdfsdf", UTF8Encoding.UTF8);
        xmlSet.WriteStartDocument();
        while (reader.Read())
        {
            switch (reader.NodeType)
            {
                case XmlNodeType.Element:
                    xmlSet.WriteStartElement(reader.Name);
                    break;

                case XmlNodeType.Text: 
                    xmlSet.WriteValue(reader.Value);
                    break;

                case XmlNodeType.CDATA: 
                    xmlSet.WriteCData(reader.Value);
                    break;

                case XmlNodeType.EndElement: 
                    xmlSet.WriteEndElement();
                    break;

                **case XmlNodeType.Attribute: 
                    xmlSet.WriteAttributeString(reader.Name, reader.Value);
                    break;**

            }
        }
        xmlSet.WriteEndDocument();
        xmlSet.Close();


What I have tried:

I dont know what should I do............
Posted
Updated 19-Jun-20 16:05pm
Comments
PIEBALDconsult 19-Jun-20 18:31pm    
You ask about reading, but the code is writing.
No one is going to go to that URL, please show a snippet of the data you are interested in.
Member 14769019 19-Jun-20 18:36pm    
If I can read attr. value, I will write. But I can't get value.

1 solution

I'm pretty sure you can't do 'attributes' like that since they are 'sub-elements'.. so, instead of
case XmlNodeType.Element:
    xmlSet.WriteStartElement(reader.Name);
    break;
I think you need something more like this
case XmlNodeType.Element:
xmlSet.WriteStartElement(reader.Name);
if(reader.HasAttributes)
{
  while(reader.MoveToNextAttribute())
  {
    // Do something with reader.Name, reader.value
  }
}
break;


you'll need to check intellisense/fill in the gaps, that's just from a quick brain dump

[Edit] you might need this
if(reader.MoveToFirstAttribute)
{
  // Do something with reader.Name, reader.value
  while(reader.MoveToNextAttribute())
  {
    // Do something with reader.Name, reader.value
  }
}
[/Edit]
 
Share this answer
 
v2
Comments
Member 14769019 19-Jun-20 22:17pm    
Yeah thank you its working.
Maciej Los 22-Jun-20 5:12am    
5ed!

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