A cross-platform UI framework for .NET
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

45 lines
1.4 KiB

using System;
using System.Collections.Generic;
using System.Xml;
using Microsoft.CodeAnalysis;
namespace XamlNameReferenceGenerator
{
internal class NameReferenceXamlParser
{
public List<(string TypeName, string Name)> GetNamedControls(AdditionalText additionalText)
{
var xaml = additionalText.GetText()!.ToString();
var document = new XmlDocument();
document.LoadXml(xaml);
var names = new List<(string TypeName, string Name)>();
IterateThroughAllNodes(document, node =>
{
var type = node.Name;
var name = node.Attributes?["x:Name"]?.Value ??
node.Attributes?["Name"]?.Value;
if (!string.IsNullOrWhiteSpace(name))
names.Add((type, name));
});
return names;
}
private static void IterateThroughAllNodes(XmlDocument doc, Action<XmlNode> elementVisitor)
{
foreach (XmlNode node in doc.ChildNodes)
{
IterateNode(node, elementVisitor);
}
}
private static void IterateNode(XmlNode node, Action<XmlNode> elementVisitor)
{
elementVisitor(node);
foreach (XmlNode childNode in node.ChildNodes)
{
IterateNode(childNode, elementVisitor);
}
}
}
}