在C#中如何将数据写入XML的指定位置?
<PrizeInfo><Prize_1Name>Jackpot1</Prize_1Name><Prize_1Money>13000</Prize_1Money><Priz...
<PrizeInfo>
<Prize_1Name>Jackpot 1</Prize_1Name>
<Prize_1Money>13000</Prize_1Money>
<Prize_1Rop>56 Rop</Prize_1Rop>
<Prize_1Background>255,0,255,0</Prize_1Background>
<Prize_1Foreground>255,0,0,0</Prize_1Foreground>
<Prize_2Name>Jackpot 2</Prize_2Name>
<Prize_2Money>4000</Prize_2Money>
<Prize_2Rop>51 Rop</Prize_2Rop>
<Prize_2Background>255,255,128,0</Prize_2Background>
<Prize_2Foreground>255,255,255,255</Prize_2Foreground>
<Prize_3Name>Gobiten</Prize_3Name>
<Prize_3Money>2000</Prize_3Money>
<Prize_3Rop>51 Rop</Prize_3Rop>
<Prize_3Background>255,128,128,255</Prize_3Background>
<Prize_3Foreground>255,0,0,0</Prize_3Foreground>
<Prize_4Name>TILBUD</Prize_4Name>
<Prize_4Money>Gratis Kaffe</Prize_4Money>
<Prize_4Rop>og en bolle</Prize_4Rop>
<Prize_4Background>255,255,255,0</Prize_4Background>
<Prize_4Foreground>255,0,0,0</Prize_4Foreground>
</PrizeInfo>
就是这个XML,比如我想只修改<Prize_1Name>里面的值,我该怎么办? 展开
<Prize_1Name>Jackpot 1</Prize_1Name>
<Prize_1Money>13000</Prize_1Money>
<Prize_1Rop>56 Rop</Prize_1Rop>
<Prize_1Background>255,0,255,0</Prize_1Background>
<Prize_1Foreground>255,0,0,0</Prize_1Foreground>
<Prize_2Name>Jackpot 2</Prize_2Name>
<Prize_2Money>4000</Prize_2Money>
<Prize_2Rop>51 Rop</Prize_2Rop>
<Prize_2Background>255,255,128,0</Prize_2Background>
<Prize_2Foreground>255,255,255,255</Prize_2Foreground>
<Prize_3Name>Gobiten</Prize_3Name>
<Prize_3Money>2000</Prize_3Money>
<Prize_3Rop>51 Rop</Prize_3Rop>
<Prize_3Background>255,128,128,255</Prize_3Background>
<Prize_3Foreground>255,0,0,0</Prize_3Foreground>
<Prize_4Name>TILBUD</Prize_4Name>
<Prize_4Money>Gratis Kaffe</Prize_4Money>
<Prize_4Rop>og en bolle</Prize_4Rop>
<Prize_4Background>255,255,255,0</Prize_4Background>
<Prize_4Foreground>255,0,0,0</Prize_4Foreground>
</PrizeInfo>
就是这个XML,比如我想只修改<Prize_1Name>里面的值,我该怎么办? 展开
展开全部
给你个启发吧
public void UpdateValue(string nKey, string nValue)
{
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.LoadXml("<PrizeInfo><Prize_1Name>Jackpot 1</Prize_1Name><Prize_1Money>13000</Prize_1Money><Prize_1Rop>56 Rop</Prize_1Rop><Prize_1Background>255,0,255,0</Prize_1Background><Prize_1Foreground>255,0,0,0</Prize_1Foreground></PrizeInfo>");
XmlNodeList elemList = xmlDoc.GetElementsByTagName(nKey);
XmlNode mNode = elemList[0];
mNode.InnerText = nValue;
XmlTextWriter xw = new XmlTextWriter(new StreamWriter(@"D:\model.xml"));
xw.Formatting = Formatting.Indented;
xmlDoc.WriteTo(xw);
xw.Close();
}
我加载了你的XML第一个对象,然后更改了里面的内容,生成出来了
当然你可以只更改而不生成,
覆盖原来的XML就行了
public void UpdateValue(string nKey, string nValue)
{
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.LoadXml("<PrizeInfo><Prize_1Name>Jackpot 1</Prize_1Name><Prize_1Money>13000</Prize_1Money><Prize_1Rop>56 Rop</Prize_1Rop><Prize_1Background>255,0,255,0</Prize_1Background><Prize_1Foreground>255,0,0,0</Prize_1Foreground></PrizeInfo>");
XmlNodeList elemList = xmlDoc.GetElementsByTagName(nKey);
XmlNode mNode = elemList[0];
mNode.InnerText = nValue;
XmlTextWriter xw = new XmlTextWriter(new StreamWriter(@"D:\model.xml"));
xw.Formatting = Formatting.Indented;
xmlDoc.WriteTo(xw);
xw.Close();
}
我加载了你的XML第一个对象,然后更改了里面的内容,生成出来了
当然你可以只更改而不生成,
覆盖原来的XML就行了
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询