1
我需要創建配置程序,其中讀/寫XML配置。配置的 一部分是以下形式:選擇兩個系統屬性之一
<camera>
<id>1</id>
<name>Camera 1</name>
<address>http://192.168.1.100</address>
<roi>
<rect>
<x>100</x>
<y>200</y>
<width>300</width>
<height>150</height>
</rect>
<rect>
<x>350</x>
<y>400</y>
<width>200</width>
<height>250</height>
</rect>
</roi>
</camera>
但輸出我需要與XML格式屬性:
<camera id="1" name="Camera 1" address="http://192.168.1.100">
<roi>
<rect x="100" y="200" width="300" height="150 />
<rect x="350" y="400" width="200" height="250 />
</roi>
</camera>
創建爲每個主節點的類,但我不知道如何選擇反序列化屬性是否應該是XmlElement,序列化應該是XmlAttribute。或者,我是否爲第一種形式的xml和第二種形式創建了兩個單獨的類?我是C#和.NET的初學者,所以還有其他方面的建議和意見嗎?
C#代碼:
[System.Serializable()]
public class CamerasConfigAttrib
{
private int id;
private string name;
private string address;
private Collection<Rectangle> roi;
[XmlAttribute("id", Form = XmlSchemaForm.Unqualified)]
public int Id
{
get { return id; }
set { id = value; }
}
[XmlAttribute("name", Form = XmlSchemaForm.Unqualified)]
public string Name
{
get { return name; }
set { name = value; }
}
[XmlAttribute("address", Form = XmlSchemaForm.Unqualified)]
public string Address
{
get { return address; }
set { address = value; }
}
[XmlArray("roi", Form = XmlSchemaForm.Unqualified)]
[XmlArrayItem("rect", typeof(Rectangle), Form = XmlSchemaForm.Unqualified]
public Collection<Rectangle> Roi
{
get { return roi; }
set
{
foreach (var rect in value)
roi.Add(rect);
}
}
}
,我認爲你必須作出兩個不同的類,但如果有更好的方式來做到這一點,我感興趣 – GGO
您應該創建一個XSLT並使用'XslCompiledTransform'來創建新的XML。 – Sefe