xml file contents are following
xml contents C# code is following:
enter image description here This property enables you to determine the difference between the following:
(IsEmptyElement is true).
(IsEmptyElement is false, although element content is empty).
A corresponding EndElement node is not generated for empty elements.
If default content has been added to an element due to schema validation, IsEmptyElement still returns true. It has no bearing on whether or not the element has a default value. In other words, IsEmptyElement simply reports whether or not the element in the source document had an end element tag.
I'm uisng net framework 4.7.2
I'm not sure why did I got these errors.
If you can help me, I will appreciate you.
Thanks.
wanna to get true value about the <item1 num="123"/>
but I can't get true value from self closing tag.
I want true value
CodePudding user response:
Property XmlReader.IsEmptyElement
is true
when the current element has no value and no attributes, and <item1 num="123"/>
has an attribute.
If you want to check whether an element has a non-empty value (element always has empty value when it's self-closing), you may use XmlReader.HasValue
.
UPDATE: if you want to check if an element is self-closing and has attributes, you may use this condition: !(xmlReader.HasValue) && xmlReader.HasAttributes
. Note that it all was described in the properties docs for XmlReader
.
CodePudding user response:
Strange, for me i got true value for :
static void Main()
{
String xml = "<book><title>bb</title><price>111</price><misc/><item num=\"123\"></item><item1 num=\"123\"/></book>";
String ff = "";
XmlReader reader = XmlReader.Create(new StringReader(xml));
while(reader.Read())
{
ff = "<";
ff = reader.Name;
if (reader.IsEmptyElement)
{
ff = " />\n";
}
else { ff = "> \n"; }
}
Console.WriteLine(ff);
}
printed:
<book>
<title>
<>
<title>
<price>
<>
<price>
<misc />
<item>
<item>
<item1 />
<book>