是否可以使用 Xdocument 创建以下 XML 并在后续 go(C#3.0) 中附加相关部分
<?xml version='1.0' encoding='UTF-8'?>
<StockMarket>
<StockDate Day = "02" Month="06" Year="2010">
<Stock>
<Symbol>ABC</Symbol>
<Amount>110.45</Amount>
</Stock>
<Stock>
<Symbol>XYZ</Symbol>
<Amount>366.25</Amount>
</Stock>
</StockDate>
<StockDate Day = "03" Month="06" Year="2010">
<Stock>
<Symbol>ABC</Symbol>
<Amount>110.35</Amount>
</Stock>
<Stock>
<Symbol>XYZ</Symbol>
<Amount>369.70</Amount>
</Stock>
</StockDate>
</StockMarket>
到目前为止,我的方法是
XDocument doc =
new XDocument(
new XElement("StockMarket",
new XElement("StockDate", new XAttribute("Day", "02"),new XAttribute("Month","06"),new XAttribute("Year","2010")),
new XElement("Stock", new XElement("Symbol", "ABC"), new XElement("Amount", "110.45"))
)
);
由于我是 Linq to XML 的新手,目前我遇到了很多困难,因此寻求帮助。
我需要的是以编程方式生成上述 XML(我的意思是说某种循环...)值将从文本框传递,因此将在运行时填充。
目前我所做的是一种静态的。这整个事情必须在运行时完成。
我面临的另一个问题是第二次保存记录时。
假设我第一次执行代码并保存它(使用我已经完成的程序)。下次当我尝试保存时,只
<StockDate Day = "xx" Month="xx" Year="xxxx">
<Stock>
<Symbol>ABC</Symbol>
<Amount>110.45</Amount>
</Stock>
<Stock>
<Symbol>XYZ</Symbol>
<Amount>366.25</Amount>
</Stock>
</StockDate>
应该保存(或更好地附加),而不是
因为它仅在第一次生成或创建 XML 时创建。
我希望我能够正确地传达我的问题。如果你们难以理解我的情况,请告诉我。
使用C#3.0。
谢谢
<?xml version='1.0' encoding='UTF-8'?>
<StockMarket>
<StockDate Day = "02" Month="06" Year="2010">
<Stock>
<Symbol>ABC</Symbol>
<Amount>110.45</Amount>
</Stock>
<Stock>
<Symbol>XYZ</Symbol>
<Amount>366.25</Amount>
</Stock>
</StockDate>
<StockDate Day = "03" Month="06" Year="2010">
<Stock>
<Symbol>ABC</Symbol>
<Amount>110.35</Amount>
</Stock>
<Stock>
<Symbol>XYZ</Symbol>
<Amount>369.70</Amount>
</Stock>
</StockDate>
</StockMarket>
My approach so far is
XDocument doc =
new XDocument(
new XElement("StockMarket",
new XElement("StockDate", new XAttribute("Day", "02"),new XAttribute("Month","06"),new XAttribute("Year","2010")),
new XElement("Stock", new XElement("Symbol", "ABC"), new XElement("Amount", "110.45"))
)
);
Since I am new to Linq to XML, I am presently struggling a lot and henceforth seeking for help.
What I need is to generate the above XML programatically(I mean to say that some kind of looping...) The values will be passed from textbox and hence will be filled at runtime.
At present the one which I have done is a kind of static one. This entire stuff has to be done at runtime.
One more problem that I am facing is at the time of saving the record for the second time.
Suppose first time I executed the code and saved it(Using the program I have done). Next time when I will try to save then only
<StockDate Day = "xx" Month="xx" Year="xxxx">
<Stock>
<Symbol>ABC</Symbol>
<Amount>110.45</Amount>
</Stock>
<Stock>
<Symbol>XYZ</Symbol>
<Amount>366.25</Amount>
</Stock>
</StockDate>
should get save(or better appended) and not <StockMarket> .... </StockMarket>..
Because that will be created only at the first time when the XML will be generated or created.
I hope I am able to convey my problem properly. If you people is having difficulty in understanding my situation, kindly let me know.
Using C#3.0 .
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
创建文档后,您可以轻松添加元素和属性:
此外,要在保存文件后添加新元素,您可以使用 XDocument.Load 然后使用与上面相同的方法将新库存添加到末尾,然后再次保存。
如果您预计文件会变得相当大,那么 XML 可能不是最佳选择,但如果您预计数据不会太多,那么它可能就可以了。
You can add elements and attributes once the document is created easy enough:
Also, to add new elements after you've saved the file, you can load it back with XDocument.Load then use the same as above to add new stocks to the end before saving it again.
If you're expecting the file to get rather large, then XML may not be the best option, but if you're not expecting too much data then it's probably OK.