mirror of
https://github.com/ICDSystems/ICD.Common.Utils.git
synced 2026-04-12 12:07:05 +00:00
fix: Potential fixes for over-reading XML
This commit is contained in:
parent
12ee533cbb
commit
a3e548290f
1 changed files with 34 additions and 7 deletions
|
|
@ -101,18 +101,37 @@ namespace ICD.Common.Utils.Xml
|
||||||
[PublicAPI]
|
[PublicAPI]
|
||||||
public virtual T ReadXmlTyped(IcdXmlReader reader)
|
public virtual T ReadXmlTyped(IcdXmlReader reader)
|
||||||
{
|
{
|
||||||
|
// Read into the first node
|
||||||
|
if (!reader.Read())
|
||||||
|
throw new FormatException();
|
||||||
|
|
||||||
T output = default(T);
|
T output = default(T);
|
||||||
bool instantiated = false;
|
bool instantiated = false;
|
||||||
|
|
||||||
while (reader.Read())
|
while (reader.NodeType != XmlNodeType.EndElement)
|
||||||
{
|
{
|
||||||
if (reader.NodeType == XmlNodeType.Element && reader.IsEmptyElement)
|
|
||||||
break;
|
|
||||||
|
|
||||||
if (!instantiated)
|
if (!instantiated)
|
||||||
{
|
{
|
||||||
instantiated = true;
|
switch (reader.NodeType)
|
||||||
output = Instantiate();
|
{
|
||||||
|
case XmlNodeType.Element:
|
||||||
|
if (reader.IsEmptyElement)
|
||||||
|
return default(T);
|
||||||
|
|
||||||
|
output = Instantiate();
|
||||||
|
instantiated = true;
|
||||||
|
|
||||||
|
// Read out of the root element
|
||||||
|
if (!reader.Read())
|
||||||
|
throw new FormatException();
|
||||||
|
continue;
|
||||||
|
|
||||||
|
default:
|
||||||
|
// Keep reading until we reach the root element.
|
||||||
|
if (!reader.Read())
|
||||||
|
throw new FormatException();
|
||||||
|
continue;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
switch (reader.NodeType)
|
switch (reader.NodeType)
|
||||||
|
|
@ -124,6 +143,14 @@ namespace ICD.Common.Utils.Xml
|
||||||
case XmlNodeType.Element:
|
case XmlNodeType.Element:
|
||||||
ReadElement(reader, output);
|
ReadElement(reader, output);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
|
case XmlNodeType.EndElement:
|
||||||
|
return output;
|
||||||
|
|
||||||
|
default:
|
||||||
|
if (!reader.Read())
|
||||||
|
return output;
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -149,7 +176,7 @@ namespace ICD.Common.Utils.Xml
|
||||||
protected virtual void ReadElement(IcdXmlReader reader, T instance)
|
protected virtual void ReadElement(IcdXmlReader reader, T instance)
|
||||||
{
|
{
|
||||||
// Skip the element
|
// Skip the element
|
||||||
reader.ReadOuterXml();
|
reader.Skip();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue