abu*_*ind 2 c# xml file-extension
我有几个XML文件,其后缀不是.XML,但.component现在我想要处理他们在C#程序,但它似乎C#中甚至不能找到这些XML文件的根元素
var doc = new XmlDocument();
doc.Load(path); // MG: edited to Load based on comment
XmlNode root = doc.SelectSingleNode("rootNodename");
Run Code Online (Sandbox Code Playgroud)
似乎根是空的,我该如何应对呢?
鉴于你已经解决了Load/ LoadXml混乱,我希望问题是命名空间; 你有例子xml吗?处理带命名空间的xml会......"有趣";-p
例如:
XmlDocument doc = new XmlDocument();
doc.LoadXml(@"<test xmlns='myFunkyUri' value='abc'/>");
// wrong; no namespace consideration
XmlElement root = (XmlElement)doc.SelectSingleNode("test");
Console.WriteLine(root == null ? "(no root)" : root.GetAttribute("value"));
// right
XmlNamespaceManager nsmgr = new XmlNamespaceManager(doc.NameTable);
nsmgr.AddNamespace("x", "myFunkyUri"); // x is my alias for myFunkyUri
root = (XmlElement)doc.SelectSingleNode("x:test", nsmgr);
Console.WriteLine(root == null ? "(no root)" : root.GetAttribute("value"));
Run Code Online (Sandbox Code Playgroud)
请注意,即使您的xml声明了xml别名,您仍可能需要为命名空间管理器重新声明它们.