我应该如何根据我的循环参数重新定义父<address>
节点并使用属性向其添加子<addressline line="">
。
目前我只将数据添加到<address>
节点。
当前代码:
public class PDFXml {
public string Filename { get; set; }
public string Filepath { get; set; }
public string Name { get; set; }
//redefine Address node and add children
public string Address { get; set; }
public int PostIndex { get; set; }
public void CreateXML(string pdfPath, string savePath) {
HeaderInfo headerinfo = new HeaderInfo();
var pdfxmldata = new PDFXml() {
Filename = Path.GetFileName(pdfPath),
Filepath = pdfPath,
Name = headerinfo.GetFullName(pdfPath).Trim(),
//Address = "??????????",
PostIndex = headerinfo.GetPostIndex(pdfPath),
};
for (int i = 0; i <= 3; i++) {
//Add addressline element to <address> - <addressline line = "i"></addressline>
}
var filestream = new FileStream(savePath + @"\" + Path.GetFileNameWithoutExtension(pdfPath) + ".xml", FileMode.Create);
new XmlSerializer(typeof(PDFXml)).Serialize(filestream, pdfxmldata);
filestream.Close();
}
}
我当前的XML
<pdfxml>
<filename></filename>
<filepath></filepath>
<name></name>
<address></address>
<index></index>
</pdfxml>
我想要这样的事情:
<pdfxml>
<filename></filename>
<filepath></filepath>
<name></name>
<address>
<addressline line="1" ></addressline>
<addressline line="2" ></addressline>
...
<addressline line="999" ></addressline>
</address>
<index></index>
</pdfxml>
答案 0 :(得分:0)
介绍新课程:
var child = new obj();
child.method();
将PDFXml的属性更改为:
public class AddressLine
{
[XmlText]
public string Text { get; set; }
[XmlAttribute("line")]
public int Line { get; set; }
}
public class Address
{
[XmlElement("addressline")]
public List<AddressLine> AddressLines { get; set; }
}
像这样创建实例:
//Changed Address to AddressL
public Address AddressL { get; set; }
按如下方式填写数据:
//Changed Address to AddressL
var pdfxmldata = new PDFXml()
{
...
AddressL = new Address { AddressLines = new List<AddressLine>() },
...
};