<Root xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"> <telegramType value="Store_In" /> <ID value="1" /> </Root> C# 用下列类接收上面xml格式 [XmlRoot("Root")] public class UaesSHSolder { [XmlElement("telegramType")] public TelegramType Telegram { get; set; } [XmlElement("ID")] public int Id { get; set; } } public class TelegramType { [XmlAttribute("value")] public string Value { get; set; } }
时间: 2024-04-28 21:27:20 浏览: 212
这个类定义是正确的,可以用于接收这个 XML 格式。其中,`XmlRoot` 标记指定了根元素的名称为 `Root`,`XmlElement` 标记指定了两个子元素 `telegramType` 和 `ID` 的名称,`TelegramType` 类定义了 `telegramType` 的属性 `value`,并用 `XmlAttribute` 标记来指定这个属性对应 XML 中的 `value` 属性。
可以通过以下代码来测试这个类的使用:
```csharp
string xml = @"<Root xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xmlns:xsd='http://www.w3.org/2001/XMLSchema'>
<telegramType value='Store_In' />
<ID value='1' />
</Root>";
XmlSerializer serializer = new XmlSerializer(typeof(UaesSHSolder));
using (StringReader reader = new StringReader(xml))
{
UaesSHSolder obj = (UaesSHSolder)serializer.Deserialize(reader);
Console.WriteLine($"Telegram Value: {obj.Telegram.Value}, ID: {obj.Id}");
}
```
输出结果为:
```
Telegram Value: Store_In, ID: 1
```
阅读全文
相关推荐



















