我不是XML专家,并且已经有好几年没有编码了。我的任务是将以下XML文件读入C#程序进行处理。我能够读取大约2个级别-直到 data index =“ 1” counter =“ 0” 行。但是,我无法读取 bit index =“ 1” value =“ false” 行。我的课有什么问题吗?
此外,如何更改类以为索引行添加name属性?
我尝试了把源XML复制到一个空C#类中并用它创建的类运行它的技巧。但是,那仍然行不通。
这是我要阅读的内容:
time value event
0 2018-01-01 00:00:00 5 A
1 2018-01-01 00:00:00 5 B
2 2018-01-01 00:10:00 15 A
3 2018-01-01 00:10:00 15 B
4 2018-01-01 00:20:00 16 A
5 2018-01-01 00:20:00 16 B
.....
17 2018-01-01 02:50:00 7 F
代码如下:
<?xml version="1.0"?>
-<mibscalar link="http://1.1.1.1/v1/mib/objs/inputPointGroupStatus?type=xml" type="readonly" name="inputPointGroupStatus">
-<index name="index1">
-<data index="1">
-<index name="index2">
-<data index="1" counter="0">
<bit index="1" value="false"/>
<bit index="2" value="false"/>
<bit index="3" value="false"/>
<bit index="4" value="false"/>
<bit index="5" value="false"/>
<bit index="6" value="false"/>
<bit index="7" value="false"/>
<bit index="8" value="false"/>
</data>
+<data index="2" counter="0">
+<data index="3" counter="0">
+<data index="4" counter="0">
+<data index="5" counter="0">
+<data index="6" counter="0">
+<data index="7" counter="0">
+<data index="8" counter="0">
+<data index="9" counter="0">
+<data index="10" counter="0">
+<data index="11" counter="0">
+<data index="12" counter="0">
+<data index="13" counter="0">
+<data index="14" counter="0">
+<data index="15" counter="0">
+<data index="16" counter="0">
</index>
</data>
+<data index="2">
+<data index="3">
+<data index="4">
+<data index="5">
+<data index="6">
+<data index="7">
+<data index="8">
+<data index="9">
+<data index="10">
</index>
</mibscalar>
这是课程:
public static AtcLoopsStatusMibData ExecuteGetLoopsStatus(string ip, string command)
{
AtcLoopsStatusMibData mib;
string uri = string.Format(API_FORMAT_STR, ip, command);
string msg = $"Calling {uri}";
Xlog.LogVerbose(msg);
try
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uri);
request.Method = "GET";
request.Timeout = 15 * 1000;
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
StreamReader reader = new StreamReader(response.GetResponseStream());
XmlSerializer serializer = new XmlSerializer(typeof(AtcLoopsStatusMibData));
mib = (AtcLoopsStatusMibData)serializer.Deserialize(reader);
}
catch (Exception ex)
{
msg = $"Call to {uri} failed: {ex.Message}";
Xlog.LogError(msg);
throw;
}
return mib;
}
这是我在调试器中看到的,并创建为输出XML文件:
[XmlRootAttribute("mibscalar")]
public class AtcLoopsStatusMibData
{
[XmlAttribute("name")]
public string Name { get; set; }
[XmlAttribute("link")]
public string Link { get; set; }
[XmlArray("index")]
[XmlArrayItem("data", typeof(Data1MibElement))]
public List<Data1MibElement> Data1List { get; set; }
[XmlElement("data")]
public Data1MibElement Data { get; set; }
/// <summary>
/// Default Constructor
/// </summary>
public AtcLoopsStatusMibData()
{
Data1List = new List<Data1MibElement>();
}
/// <summary>
/// Sets the "Link" property that is required when sending Mib object
/// to ATC.
/// </summary>
/// <param name="ip">IP Address of the ATC</param>
/// <param name="commandName">Name of the API command call</param>
public void SetLinkAndName(string ip, string commandName)
{
Link = string.Format(HttpHelper.API_FORMAT_STR, ip, commandName);
Name = commandName;
}
}
/// <summary>
/// Secondary object inside the MIB returned by the ATC
/// for detector loop status.
/// This object primary
/// holds the values of the request or command.
/// </summary>
[Serializable]
public class Data1MibElement
{
[XmlAttribute("index")]
public int Index { get; set; }
[XmlArray("index")]
[XmlArrayItem("data", typeof(Data2MibElement))]
public List<Data2MibElement> Data2List { get; set; }
[XmlElement("data")]
public Data2MibElement Data { get; set; }
/// <summary>
/// Default Constructor
/// </summary>
public Data1MibElement()
{
Data2List = new List<Data2MibElement>();
}
/// <summary>
/// Constructor used when building the LoopsStatus MIB object
/// </summary>
/// <param name="index">Loop # (1-6) </param>
/// <param name="counter">Detector loop status value </param>
public Data1MibElement(int index)
{
Index = index;
}
}
/// <summary>
/// Secondary object inside the MIB returned by the ATC
/// for detector loop status.
/// This object primary
/// holds the values of the request or command.
/// </summary>
[Serializable]
public class Data2MibElement
{
[XmlAttribute("index")]
public int Index { get; set; }
[XmlAttribute("counter")]
public int Counter { get; set; }
[XmlArray("bit")]
public List<BitMibElement> BitList { get; set; }
/// <summary>
/// Default Constructor
/// </summary>
public Data2MibElement()
{
BitList = new List<BitMibElement>();
}
/// <summary>
/// Constructor used when building the LoopsStatus MIB object
/// </summary>
/// <param name="index">Loop # (1-6) </param>
/// <param name="counter">Detector loop status value </param>
public Data2MibElement(int index, int counter)
{
Index = index;
Counter = counter;
}
}
/// <summary>
/// Secondary object inside the MIB returned by the ATC
/// for detector loop status.
/// This object primary
/// holds the values of the request or command.
/// </summary>
[Serializable]
public class BitMibElement
{
[XmlAttribute("index")]
public int Index { get; set; }
[XmlAttribute("value")]
public string Value { get; set; }
/// <summary>
/// Default Constructor
/// </summary>
public BitMibElement()
{
}
/// <summary>
/// Constructor used when building the BitMibElement MIB object
/// </summary>
/// <param name="index">Loop # (1-6) </param>
/// <param name="value">Detector loop status value </param>
public BitMibElement(int index, string value)
{
Index = index;
Value = value;
}
}
不带+和-字符-在每个数据部分下复制位部分以完全查看完整文件。如果此网页可以让我插入那么长的内容,我可以为您完成。
<?xml version="1.0" encoding="UTF-8"?>
-<mibscalar link="http://1.1.1.1/v1/mib/objs/inputPointGroupStatus?type=xml" name="inputPointGroupStatus" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
-<index>
-<data index="1">
-<index>
-<data index="1" counter="0">
<bit/>
</data>
-<data index="2" counter="0">
<bit/>
</data>
-<data index="3" counter="0">
<bit/>
</data>
-<data index="4" counter="0">
<bit/>
</data>
-<data index="5" counter="0">
<bit/>
</data>
-<data index="6" counter="0">
<bit/>
</data>
-<data index="7" counter="0">
<bit/>
</data>
-<data index="8" counter="0">
<bit/>
</data>
-<data index="9" counter="0">
<bit/>
</data>
-<data index="10" counter="0">
<bit/>
</data>
-<data index="11" counter="0">
<bit/>
</data>
-<data index="12" counter="0">
<bit/>
</data>
-<data index="13" counter="0">
<bit/>
</data>
-<data index="14" counter="0">
<bit/>
</data>
-<data index="15" counter="0">
<bit/>
</data>
-<data index="16" counter="0">
<bit/>
</data>
</index>
</data>
+<data index="2">
+<data index="3">
+<data index="4">
+<data index="5">
+<data index="6">
+<data index="7">
+<data index="8">
+<data index="9">
+<data index="10">
</index>
</mibscalar>
答案 0 :(得分:1)
从XML生成c#类(例如xsd.exe
和https://xmltocsharp.azurewebsites.net/)的工具在创建使用某些给定XML的类型时非常有用,但它们有局限性。可能遇到的一个限制是它们不能正确检测递归数据结构,即直接或间接包含自身其他实例的类型。这就是你在这里的东西。
您的XML元素展示的嵌套关系如下:
<mibscalar>
具有(多个)<index>
(或<bit>
?)。<index>
具有多个<data>
。<data>
具有多个<index>
或<bit>
(或两者都有?)。此结构可以建模如下:
[XmlRoot(ElementName = "mibscalar")]
public class Mibscalar
{
[XmlAttribute(AttributeName = "link")]
public string Link { get; set; }
[XmlAttribute(AttributeName = "type")]
public string Type { get; set; }
[XmlAttribute(AttributeName = "name")]
public string Name { get; set; }
[XmlElement("bit", typeof(Bit))]
[XmlElement("index", typeof(Index))]
public List<DataItemBase> Items { get; set; }
}
public class Data
{
[XmlAttribute(AttributeName = "index")]
public string Index { get; set; }
[XmlAttribute(AttributeName = "counter")]
public string Counter { get; set; }
[XmlElement("bit", typeof(Bit))]
[XmlElement("index", typeof(Index))]
public List<DataItemBase> Items { get; set; }
}
public abstract class DataItemBase
{
}
public class Index : DataItemBase
{
[XmlAttribute(AttributeName = "name")]
public string Name { get; set; }
[XmlElement(ElementName = "data")]
public List<Data> Data { get; set; }
}
public class Bit : DataItemBase
{
[XmlAttribute(AttributeName = "index")]
public string Index { get; set; }
[XmlAttribute(AttributeName = "value")]
public string Value { get; set; }
}
我使用https://xmltocsharp.azurewebsites.net/生成初始类,然后对其进行了修改以更正父/子层次结构。
工作示例.Net在这里拨弄:https://dotnetfiddle.net/9yQ7oN。