以下のコードでは、XmlSchemaDocumentの要素を検索しています。要素がXmlSchemaDocumentの一部である場合、反復処理はうまく動作します。XmlSchemaDocumentの要素部分の再帰的検索
public void FindSchemaElement(string elementName, string dataType, List<XmlSchemaElement> allChildren, string parentName)
{
try
{
List<XmlSchemaElement> temp = new List<XmlSchemaElement>();
temp.AddRange(allChildren);
foreach (XmlSchemaElement e in allChildren)
{
if (e.Name != elementName && e.RefName.Name != elementName &&
(dataType == "" || e.SchemaTypeName.Name != dataType)) continue;
if (e.Parent == null || e.Parent is XmlSchema)
{
ElementToBeFound = e;
return;
}
var parent = e.Parent;
while (parent != null && parent.GetType() != typeof(XmlSchemaElement))
parent = parent.Parent;
if (parent != null && ((XmlSchemaElement) parent).Name == parentName)
{
ElementToBeFound = e;
return;
}
if (parent == null || parent.GetType() == typeof(XmlSchema)) ElementToBeFound = e;
}
if (ElementToBeFound != null) return;
_childrenList.Clear();
if (temp.Count > 0)
GetNextChildren(temp, dataType, elementName, parentName);
}
catch(Exception exception){Debug.WriteLine("FindSchemaElement: "+exception.Message);}
}
子供の取得:GetChildrenメソッドで
private void GetNextChildren(List<XmlSchemaElement> allChildren, string dataType, string elementName, string parentName = "")
{
try
{
foreach (XmlSchemaElement e in allChildren)
GetChildren(e);
if (parentName != string.Empty)
FindSchemaElement(elementName, dataType, _childrenList, parentName);
else
FindSubsGroups(elementName, dataType, _childrenList);
}
catch (Exception ex)
{ Debug.WriteLine("GetNextChildren: " + ex.Message); }
}
() - 私はちょうど要素(シーケンス、choice要素)の種類を確認していますし、それが要素であれば、私はそれを追加してい_childrenList
しかし、要素が見つからない場合、ループに詰まり、最終的にメモリが不足します(使用している.xsdファイルがかなり大きい)。 この問題は、.xsd内にループがある場合(図のように)に気づきました。 要素が見つからないのに反復を停止する方法はありますか?ループ?
ループ内に反復を停止するための 'break'キーワードがあります。 – Smartis
私はそれを知っています..しかし、いつループを止める必要があるのか分かりますか?すべての要素がチェックされていることを確認してから、ループを止めることができます。 – MonicaS
関数 'GetChildren'と' FindSubsGroups'は何ですか?あなたもそれらを投稿できますか? – Smartis