C # 모델을 사용하여 아래 구조의 xml 파일을 생성하려고합니다.
<root>
<row>
<field name="placeholder">Test field 1</field>
<field name="placeholder">Test field 2</field>
<field name="placeholder">Test field 3</field>
<field name="placeholder">Test field 4</field>
<field name="placeholder">Test field 5</field>
</row>
<row>
<field name="placeholder">Test field 1</field>
<field name="placeholder">Test field 2</field>
<field name="placeholder">Test field 3</field>
<field name="placeholder">Test field 4</field>
<field name="placeholder">Test field 5</field>
</row>
</root>
이 모델을 설정했지만 필요한 내용에 대해 완전히 잘못된 출력을 얻습니다.
public class root
{
public List<row> rows {get; set;}
}
public class row
{
public List<field> fields {get; set;}
}
public class field
{
[XmlAttribute("name")]
public string AttributeName { get; set; }
[XmlText]
public string AttributeValue { get; set; }
}
이것은 모델을 만드는 데 사용한 코드입니다.
var root = new root();
root.rows = new List<row>();
for (int x = 0; x < 2; x++)
{
root.rows.Add(new row()
{
fields = new List<field>()
{
new field()
{
AttributeName="placeholder",
AttributeValue="test1",
},
new field()
{
AttributeName="placeholder",
AttributeValue="test2",
},
new field()
{
AttributeName="placeholder",
AttributeValue="test3",
},
new field()
{
AttributeName="placeholder",
AttributeValue="test4",
},
new field()
{
AttributeName="placeholder",
AttributeValue="test5",
}
}
});
}
따라서 루트에는 코드에서 정의 할 수있는 여러 행이 있어야하지만 행에는 속성 이름과 값이 다른 여러 필드가 있습니다.
필드 속성과 값을 채우는 데 필요한 데이터가 루프에 있으므로 새 행은 항상 루프에 추가됩니다.
내가 직면 한 문제는 이것을 실행할 때 아래 출력을 얻는 것입니다.
<root>
<rows>
<row>
<fields>
<field name="placeholder">test1</field>
<field name="placeholder">test2</field>
<field name="placeholder">test3</field>
<field name="placeholder">test4</field>
<field name="placeholder">test5</field>
</fields>
</row>
<row>
<fields>
<field name="placeholder">test1</field>
<field name="placeholder">test2</field>
<field name="placeholder">test3</field>
<field name="placeholder">test4</field>
<field name="placeholder">test5</field>
</fields>
</row>
</rows>
</root>
다음을 사용하십시오.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Serialization;
using System.IO;
namespace ConsoleApplication1
{
class Program
{
const string FILENAME = @"c:\temp\test.xml";
static void Main(string[] args)
{
root root = new root()
{
rows = new List<row>() {
new row() {
fields = new List<field>() {
new field() { AttributeName = "placeholder", AttributeValue = "Test Field 1"},
new field() { AttributeName = "placeholder", AttributeValue = "Test Field 2"},
new field() { AttributeName = "placeholder", AttributeValue = "Test Field 3"},
new field() { AttributeName = "placeholder", AttributeValue = "Test Field 4"},
new field() { AttributeName = "placeholder", AttributeValue = "Test Field 5"}
}
},
new row() {
fields = new List<field>() {
new field() { AttributeName = "placeholder", AttributeValue = "Test Field 1"},
new field() { AttributeName = "placeholder", AttributeValue = "Test Field 2"},
new field() { AttributeName = "placeholder", AttributeValue = "Test Field 3"},
new field() { AttributeName = "placeholder", AttributeValue = "Test Field 4"},
new field() { AttributeName = "placeholder", AttributeValue = "Test Field 5"}
}
}
}
};
XmlWriterSettings settting = new XmlWriterSettings();
settting.Indent = true;
XmlWriter writer = XmlWriter.Create(FILENAME, settting);
XmlSerializer serializer = new XmlSerializer(typeof(root));
serializer.Serialize(writer, root);
}
}
public class root
{
[XmlElement(ElementName = "row")]
public List<row> rows { get; set; }
}
public class row
{
[XmlElement("field")]
public List<field> fields { get; set; }
}
public class field
{
[XmlAttribute("name")]
public string AttributeName { get; set; }
[XmlText]
public string AttributeValue { get; set; }
}
}
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다