C#解析XML并将节点存储至网格组件的技术实现问询
Alright, let's walk through how to parse that ODM XML you've got and get the data into a grid component using C#. I'll use WinForms' DataGridView as an example, but the core logic translates easily to WPF's DataGrid too.
1. 定义数据模型
First, we'll create a simple class to map the ItemData nodes—this makes it way easier to bind to the grid later:
public class ItemDataModel { public string ItemID { get; set; } public bool IsNull { get; set; } public string Value { get; set; } }
If you need to include context from parent nodes (like SubjectKey or FormID), you can extend this model later—more on that below.
2. 解析XML内容
We'll use LINQ to XML (XDocument) for clean, readable parsing. Don't forget to handle the XML namespace from your ODM root element (replace the namespace URI below with the actual one from your XML):
// Assume your raw XML string is stored in xmlContent string xmlContent = "<ODM xmlns=\"http://www.cdisc.org/ns/odm/v1.3\">...</ODM>"; // Your full XML here XDocument xmlDoc = XDocument.Parse(xmlContent); XNamespace ns = "http://www.cdisc.org/ns/odm/v1.3"; // Match the xmlns value from your XML // Extract all ItemData nodes into our model list var itemDataList = xmlDoc.Descendants(ns + "ItemData") .Select(itemNode => new ItemDataModel { ItemID = itemNode.Attribute("ItemID")?.Value, IsNull = bool.Parse(itemNode.Attribute("IsNull")?.Value ?? "False"), Value = itemNode.Attribute("Value")?.Value ?? string.Empty }) .ToList();
Pro tip: Use the null-conditional (?.) and null-coalescing (??) operators to avoid NullReferenceException if attributes are missing.
3. 绑定到网格组件
WinForms DataGridView
If you're using WinForms, binding the list to the grid is straightforward:
// Assume your grid control is named dataGridView1 dataGridView1.AutoGenerateColumns = true; // Auto-create columns from the model dataGridView1.DataSource = itemDataList;
For more control over column appearance, manually define columns instead:
dataGridView1.Columns.Clear(); dataGridView1.Columns.Add(new DataGridViewTextBoxColumn { DataPropertyName = "ItemID", HeaderText = "Item ID", Width = 100 }); dataGridView1.Columns.Add(new DataGridViewCheckBoxColumn { DataPropertyName = "IsNull", HeaderText = "Is Null?", Width = 80 }); dataGridView1.Columns.Add(new DataGridViewTextBoxColumn { DataPropertyName = "Value", HeaderText = "Item Value", AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill }); dataGridView1.DataSource = itemDataList;
WPF DataGrid
For WPF, the logic is nearly identical. Just set the ItemsSource property:
// Assume your DataGrid is named dataGrid1 dataGrid1.ItemsSource = itemDataList;
You can also define columns in XAML for better UI control.
4. 包含父节点上下文(可选)
If you need to display additional context like which Subject or Form each ItemData belongs to, extend the model and adjust the parsing:
public class ExtendedItemDataModel { public string SubjectKey { get; set; } public string FormID { get; set; } public string ItemGroupID { get; set; } public string ItemID { get; set; } public bool IsNull { get; set; } public string Value { get; set; } }
Then parse with parent node attributes included:
var extendedItemList = xmlDoc.Descendants(ns + "SubjectData") .SelectMany(subjectNode => subjectNode.Descendants(ns + "FormData") .SelectMany(formNode => formNode.Descendants(ns + "ItemGroupData") .SelectMany(groupNode => groupNode.Descendants(ns + "ItemData") .Select(itemNode => new ExtendedItemDataModel { SubjectKey = subjectNode.Attribute("SubjectKey")?.Value, FormID = formNode.Attribute("FormID")?.Value, ItemGroupID = groupNode.Attribute("ItemGroupID")?.Value, ItemID = itemNode.Attribute("ItemID")?.Value, IsNull = bool.Parse(itemNode.Attribute("IsNull")?.Value ?? "False"), Value = itemNode.Attribute("Value")?.Value ?? string.Empty }) ) ) ) .ToList();
Bind this extended list to your grid, and you'll have all the contextual data alongside the ItemData values.
内容的提问来源于stack exchange,提问作者aggicd

