代码的目的是读取xml元素并显示datagridview中相关列名下的每个元素。
所以,我在这里有我的代码:
IEnumerable<XElement> tables = xelement.Elements(df + "Table");
foreach (XElement table in tables)
{
//Get name from TableName node
XElement tableNameNode = table.Element(df + "TableName");
tbTableName.Text = tableNameNode.Value.ToString();
XElement numberRows = table.Element(df + "NumberOfRows");
tbNumRows.Text = numberRows.Value.ToString();
string tableName = tableNameNode.Value;
TableData td = new TableData(CurrentProject.CurrentSchema.FindTable(tableName));
td.PopulateFieldData();
// tableDataList.TryGetValue(tableName, out td);
tableDataList.Add(tableName, td);
//If you open the project after you save it or open it, exception throws: Already Added Key!
IEnumerable<XElement> fields = table.Elements(df + "Field");
foreach (XElement field in fields)
{
XElement fieldNameNode = field.Element(df + "Name");
string fieldName = fieldNameNode.Value;
FieldData fd = td.FieldList[fieldName];
IEnumerable<XElement> fieldProps = field.Descendants();
foreach (XElement fieldProp in fieldProps)
{
string fieldPropertyName = fieldProp.Name.ToString();
if (fieldPropertyName == "Name")
{
fd.Name = fieldProp.Value;
}
if (fieldPropertyName == "Type")
{
fd.DataType = fieldProp.Value;
}
if (fieldPropertyName == "Size")
{
int i = 0;
int.TryParse(fieldProp.Value.ToString(), out i);
fd.Size = i;
}
if (fieldPropertyName == "Nullable")
{
if (fieldProp.Value.ToString() == "True")
fd.Nullable = true;
else
fd.Nullable = false;
}
if (fieldPropertyName == "ContentSource")
{
fd.contentSource = fieldProp.Value;
}
if (fieldPropertyName == "ConstantValue")
{
fd.constantValue = fieldProp.Value;
}
if (fieldPropertyName == "RandomValue")
{
fd.averageSize = fieldProp.Value;
}
if (fieldPropertyName == "List")
{
fd.pickList = fieldProp.Value;
}
}
dataGridView1.Rows.Add(fd.Name, fd.DataType, fd.Size, fd.Nullable);
string ColumnName = dataGridView1.CurrentRow.Cells[0].Value.ToString();
this.tableDataList.TryGetValue(tbTableName.Text, out td);
if (td != null)
{
td.FieldList.TryGetValue(ColumnName, out fd);
}
foreach (var fdl in td.FieldList)
{
if (td != null && fd != null)
{
if (fd.contentSource == "Constant")
{
dataGridView1.CurrentRow.Cells[4].Value = fd.contentSource + "(" + "Value= " + fd.constantValue + ")";
}
if (fd.contentSource == "List")
{
dataGridView1.CurrentRow.Cells[4].Value = fd.contentSource + "(" + fd.pickList + ")";
}
if (fd.contentSource == "Random")
{
dataGridView1.CurrentRow.Cells[4].Value = fd.contentSource + "(" + fd.averageSize + ")";
}
}
}
}
}
这是我正在阅读的Xml文件:
<?xml version="1.0"?>
<GeneratorXml>
<SchemaPath>C:\Projects\CoreSchema.xml</SchemaPath>
<GroupName>Documents</GroupName>
-<Table>
<TableName>directory</TableName>
<NumberOfRows>33</NumberOfRows>
-<Field>
<Name>dir_id</Name>
<Type>number</Type>
<Size>10</Size>
<Nullable>False</Nullable>
<ContentSource>Random</ContentSource>
<ConstantValue> </ConstantValue>
<RandomValue>4</RandomValue>
<List> </List>
</Field>
-<Field>
<Name>directory_number</Name>
<Type>number</Type>
<Size>5</Size>
<Nullable>False</Nullable>
<ContentSource>List</ContentSource>
<ConstantValue> </ConstantValue>
<RandomValue> </RandomValue>
<List>Coffee</List>
</Field>
-<Field>
<Name>file_count</Name>
<Type>number</Type>
<Size>5</Size>
<Nullable>False</Nullable>
<ContentSource>Constant</ContentSource>
<ConstantValue>2</ConstantValue>
<RandomValue> </RandomValue>
<List> </List>
</Field>
+<Field>
</Table>
</GeneratorXml>
我认为我的for循环遇到了问题,但我找不到它。提前感谢您的每一个想法。