我正在使用cmarkup解析器生成sample.xml文件,如下所示:
CMarkup xml;
xml.AddElem( "ORDER" );
xml.IntoElem();
xml.AddElem( "ITEM" );
xml.IntoElem();
xml.AddElem( "SN", "132487A-J" );
xml.AddElem( "NAME", "crank casing" );
xml.AddElem( "QTY", "1" );
以下是生成的sample.xml文件:
<ORDER>
<ITEM>
<SN>132487A-J</SN>
<NAME>crank casing</NAME>
<QTY>1</QTY>
</ITEM>
</ORDER>
现在,我想添加
<?xml version="1.0" encoding="UTF-8"?>
在生成的sample.xml的顶部,并且还想在&#34; ITEM&#34;中添加一行。标记为:
...
<ITEM>
<Person age="12" passed="Yes">Alive</Person>
....
</ITEM>
我在cmarkup中搜索了各种方法来实现这一目标,但我并没有明白这一点。那么,请建议我如何在xml中添加上面提到的两行。
答案 0 :(得分:1)
public class DataLoader
{
private IEnumerable<IDataLoaded> allDataLoaded = new List<IDataLoaded>();
private IEnumerable<ILoader<IDataLoaded>> loaderLists = new
List<ILoader<IDataLoaded>>
{
new JsonLoader<RootSpacecrafts>(),
new JsonLoader<RootWeapons>()
};
public void LoadData()
{
foreach (ILoader<IDataLoaded> loader in loaderLists)
{
loader.Read();
allDataLoaded = loader.Load();
}
}
}
public interface ILoader<out T>
{
void Read();
IEnumerable<T> Load();
}
public class JsonLoader<T> : ILoader<T> where T : IDataLoaded, new()
{
private string fileContents;
private T output = new T();
private IEnumerable<T> outputlist;
public void Read()
{
fileContents = File.ReadAllText("");
outputlist = JsonConvert.DeserializeObject<List<T>>(fileContents);
}
public IEnumerable<T> Load()
{
return outputlist;
}
}