我想用一个 XML 文件生成一个 Html 代码。这是我的xml:
<XML>
<Groups>
<Group Name="Group1">
<Item ID="9A4FA56F-EAA0-49AF-B7F0-8CA09EA39167"/>
<Item ID="351FEF76-B826-426F-88C4-DBAAA60F886B"/>
<Item ID="96A4CBFC-04CD-4D27-ADE6-585C05E4DBC9"/>
<Item ID="D8876943-5861-4D62-9249-C5FEF88219FA"/>
</Group>
<Group Name="Group2">
<Item ID="9A4FA56F-EAA0-49AF-B7F0-8CA09EA39167"/>
<Item ID="351FEF76-B826-426F-88C4-DBAAA60F886B"/>
</Group>
</Groups>
<Items>
<Item>
<GUID>9A4FA56F-EAA0-49AF-B7F0-8CA09EA39167</GUID>
<Type>button</Type>
<Title>Save</Title>
<Value>submit</Value>
<Name>btnsave</Name>
<MaxLen>5</MaxLen>
</Item>
<Item>
<GUID>351FEF76-B826-426F-88C4-DBAAA60F886B</GUID>
<Type>text</Type>
<Title>Name:</Title>
<Name>txtname</Name>
<Value>Name</Value>
<MaxLen>2</MaxLen>
</Item>
<Item>
<GUID>02973DCC-5677-417C-A9BF-1578F58923EF</GUID>
<Type>text</Type>
<Title>Family:</Title>
<Name>txtFamiy</Name>
<Value>Family</Value>
<MaxLen>2</MaxLen>
</Item>
<Item>
<GUID>96A4CBFC-04CD-4D27-ADE6-585C05E4DBC9</GUID>
<Type>checkbox</Type>
<Title>I agree to the terms.</Title>
<Name>chkagree</Name>
<Value>Agree</Value>
<MaxLen>10</MaxLen>
</Item>
<Item>
<GUID>D8876943-5861-4D62-9249-C5FEF88219FA</GUID>
<Type>select</Type>
<Title>Type of property</Title>
<Name>PropertyType</Name>
<Value></Value>
</Item>
我遇到的问题是我需要为每个组创建一个字段集标记,并且相关元素必须在该字段集中。
这是我的 C# 代码:
XmlTextReader reader = new XmlTextReader(xmlfileaddress);
Group objGroup = new Group();
while (reader.Read())
{
switch (reader.Name)
{
case "Groups":
while (reader.Read())
{
if (reader.NodeType == XmlNodeType.EndElement)
break;
switch (reader.Name)
{
case "Group":
if (reader.IsStartElement())
{
}
while (reader.Read())
{
if (reader.NodeType == XmlNodeType.EndElement)
break;
switch (reader.Name)
{
case "Item":
objGroup.ItemIDs.Add(new Guid());
break;
}
}
break;
}
}
break;
case "Items":
Item objItem = new Item();
while (reader.Read())
{
if (reader.NodeType == XmlNodeType.EndElement)
break;
switch (reader.Name)
{
case "Item":
while (reader.Read())
{
if (reader.NodeType == XmlNodeType.EndElement)
break;
switch (reader.Name)
{
case "GUID":
objItem.Id = reader.ReadElementContentAsString();
break;
case "Title":
objItem.Title = reader.ReadElementContentAsString();
break;
case "Type":
objItem.Type = reader.ReadElementContentAsString();
break;
case "Value":
objItem.Value = reader.ReadElementContentAsString();
break;
case "Name":
objItem.Name = reader.ReadElementContentAsString();
break;
case "MaxLen":
objItem.MaxLen = reader.ReadElementContentAsString();
break;
}
}