如何在xml文件中插入值

时间:2022-07-26 22:49:08

I am using an xml files and whose content is like below

我正在使用xml文件,其内容如下所示

<Invoices>
   <Invoice>
      <Type>[Type]</Type>
      <Contact></contact>
   </Invoice>
<Invoices>

and in my class I want to fill the xml ontent with the data coming in the object I want to replace [type] in <Type>[Type]</Type> with obj.type how can I achieve this. Just want an idea.

在我的班级中,我想用xj ontent填充对象中的数据,我希望用obj.type替换 [Type] 中的[type],如何实现这一点。只是想要一个主意。

This is my code:

这是我的代码:

foreach (XmlNode pnode in xmlParentNode)
{
    pnode.InnerText = objInvoice.Invoice_type;
    xmlRequestNode = pnode.SelectNodes("Contact");
    // var app = xdoc.Root.Descendants("Appliance").SingleOrDefault(e => (string)e.Element("Name") == applianceName);
    foreach (XmlNode item in xmlRequestNode)
    {
        if (item.Name == "ContactNumber")
        {
            item.InnerText = objInvoice.ContactNumber.ToString();
        }
    }
}

Thanks in advance.

提前致谢。

2 个解决方案

#1


1  

How about this:

这个怎么样:

XmlDocument doc = new XmlDocument();
doc.LoadXml(xmlString);

foreach (XmlNode typeNode in doc.SelectNodes("/Invoices/Invoice/Type[. = '[Type]']"))
{
    typeNode.InnerText= obj.type;
}

string modifiedXml = doc.OuterXml;

#2


1  

XmlDocument doc = new XmlDocument();
doc.Load(path);

foreach (XmlNode node in doc.GetElementsByTagName("Type[. = '[Type]']"))
    node.InnerText = "[Obj.Type]";

doc.Save(path);

#1


1  

How about this:

这个怎么样:

XmlDocument doc = new XmlDocument();
doc.LoadXml(xmlString);

foreach (XmlNode typeNode in doc.SelectNodes("/Invoices/Invoice/Type[. = '[Type]']"))
{
    typeNode.InnerText= obj.type;
}

string modifiedXml = doc.OuterXml;

#2


1  

XmlDocument doc = new XmlDocument();
doc.Load(path);

foreach (XmlNode node in doc.GetElementsByTagName("Type[. = '[Type]']"))
    node.InnerText = "[Obj.Type]";

doc.Save(path);