是否可以使用 Xdocument 创建以下 XML 并在后续 go(C#3.0) 中附加相关部分

发布于 2024-09-04 04:46:42 字数 2091 浏览 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

白昼 2024-09-11 04:46:42

创建文档后,您可以轻松添加元素和属性:

XDocument doc = /* what you had already */

// get the "<StockMarket>" element
var stockMarketElem = doc.Element("StockMarket");

// add a new StockDate to it in a loop
for(var dt = new DateTime(2010, 1, 1); dt <= new DateTime(2010, 6, 1); dt = dt.AddDays(1))
{
    stockMarketElem.Add(
      new XElement("StockDate",
        new XAttribute("Day", dt.Day), new XAttribute("Month", dt.Month), ...
    );
}

此外,要在保存文件后添加新元素,您可以使用 XDocument.Load 然后使用与上面相同的方法将新库存添加到末尾,然后再次保存。

如果您预计文件会变得相当大,那么 XML 可能不是最佳选择,但如果您预计数据不会太多,那么它可能就可以了。

You can add elements and attributes once the document is created easy enough:

XDocument doc = /* what you had already */

// get the "<StockMarket>" element
var stockMarketElem = doc.Element("StockMarket");

// add a new StockDate to it in a loop
for(var dt = new DateTime(2010, 1, 1); dt <= new DateTime(2010, 6, 1); dt = dt.AddDays(1))
{
    stockMarketElem.Add(
      new XElement("StockDate",
        new XAttribute("Day", dt.Day), new XAttribute("Month", dt.Month), ...
    );
}

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文