C# 根据目录中XML文件的数量创建多个XmlDocument对象 我在目录中有多个XML文件,每个目录都可以考虑为每个场景。根据业务场景,目录中XML文件的数量可能会有所不同
从下面的代码中,我得到了文件总数C# 根据目录中XML文件的数量创建多个XmlDocument对象 我在目录中有多个XML文件,每个目录都可以考虑为每个场景。根据业务场景,目录中XML文件的数量可能会有所不同,c#,xml,C#,Xml,从下面的代码中,我得到了文件总数 string tempPath = rootPath+"/Templates"; // Template directory path DirectoryInfo d = new DirectoryInfo(@tempPath); FileInfo[] files = d.GetFiles("*.xml"); // getting all file names 我正在尝试为xml设置名称空间,如下代码所示 //Setting namespace for eac
string tempPath = rootPath+"/Templates"; // Template directory path
DirectoryInfo d = new DirectoryInfo(@tempPath);
FileInfo[] files = d.GetFiles("*.xml"); // getting all file names
我正在尝试为xml设置名称空间,如下代码所示
//Setting namespace for each xml
foreach(FileInfo file in files)
{
var tempXml = file.Name;
XmlDocument tempXml = new XmlDocument();
tempXml.Load(tempPath+"/"+file.Name);
XmlNamespaceManager nsMgr = new XmlNamespaceManager(tempXml.NameTable);
nsMgr.AddNamespace("imp", namespace1); // namespace1 value I took it from excel
nsMgr.AddNamespace("soapenv", "http://schemas.xmlsoap.org/soap/envelope/");
}
后来我尝试像这样在xml文档中插入值
XmlNode businessContactNumber = doc.SelectSingleNode(xPath,nsMgr);
上面的代码不起作用,我知道我在代码中犯了一些严重的错误。我不熟悉这个C代码,请帮助我解决这个问题。使用XML Linq
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;
using System.IO;
namespace ConsoleApplication62
{
class Program
{
const string FOLDER = @"\c:\temp";
static void Main(string[] args)
{
string[] filenames = Directory.GetFiles(FOLDER);
foreach (string file in filenames)
{
XDocument tempXml = XDocument.Load(file);
XElement root = (XElement)tempXml.FirstNode;
XNamespace nsMgr = root.Name.Namespace;
XElement node = root.Descendants(nsMgr + "TagName").FirstOrDefault();
}
}
}
}
代码中不清楚您面临什么问题或最终目标是什么。你有什么例外?