XML文件的根节点有太多的属性,用XmlDocument.Load读文件时,读不到子节点??

xundeng 2010-10-15 10:10:37
XML文件的根节点有太多的属性如下所示的 SCL节点.

<?xml version="1.0" encoding="UTF-8"?>
<SCL xmlns="http://www.iec.ch/61850/2003/SCL" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance ./xsd/SCL.xsd" xsi:schemaLocation="http://www.iec.ch/61850/2003/SCL .\xsd\SCL.xsd" xmlns:ext="http://nari-relays.com">
<Header id="template" version="R6" revision="reversion" toolID="toolID" nameStructure="IEDName">
<History>
<Hitem version="1.0" revision="0.1" when="2010/10/13 19:35:28" why="update the file"></Hitem>
<Hitem version="1.0" revision="0.2" when="2010/10/14 09:00:07" why="update the file"></Hitem>
<Hitem version="1.0" revision="0.3" when="2010/10/14 13:28:49" why="update the file"></Hitem>
</History>
</Header>


用XmlDocument.Load时读不到根节点下面的子节点,有什么办法???
如果把根节点的属性都去掉变成

<?xml version="1.0" encoding="UTF-8"?>
<SCL xmlns="http://www.iec.ch/61850/2003/SCL" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance ./xsd/SCL.xsd" xsi:schemaLocation="http://www.iec.ch/61850/2003/SCL .\xsd\SCL.xsd" xmlns:ext="http://nari-relays.com">
<Header id="template" version="R6" revision="reversion" toolID="toolID" nameStructure="IEDName">
<History>
<Hitem version="1.0" revision="0.1" when="2010/10/13 19:35:28" why="update the file"></Hitem>
<Hitem version="1.0" revision="0.2" when="2010/10/14 09:00:07" why="update the file"></Hitem>
<Hitem version="1.0" revision="0.3" when="2010/10/14 13:28:49" why="update the file"></Hitem>
</History>
</Header>

就可以读到下面的子节点了.
现在的问题是XML文本不能修改.能不能修改XmlDocument的属性使它能支持N多属性的根节点.
...全文
685 16 打赏 收藏 转发到动态 举报
写回复
用AI写文章
16 条回复
切换为时间正序
请发表友善的回复…
发表回复
xundeng 2010-10-15
  • 打赏
  • 举报
回复
OXmlDoct = new XmlDocument();
XmlNamespaceManager nsmgr = null;
OXmlDoct.Load(cidpath.Text);
nsmgr = new XmlNamespaceManager(OXmlDoct.NameTable);
nsmgr.AddNamespace("ab", "http://www.iec.ch/61850/2003/SCL");
NoList = OXmlDoct.DocumentElement.SelectNodes("ab:IED", nsmgr)

这样就搞定了.
claymore1114 2010-10-15
  • 打赏
  • 举报
回复
属性名字 怎么设的啊
孟子E章 2010-10-15
  • 打赏
  • 举报
回复
去除方法
//Implemented based on interface, not part of algorithm
public static string RemoveAllNamespaces(string xmlDocument)
{
XElement xmlDocumentWithoutNs = RemoveAllNamespaces(XElement.Parse(xmlDocument));

return xmlDocumentWithoutNs.ToString();
}

//Core recursion function
private static XElement RemoveAllNamespaces(XElement xmlDocument)
{
if (!xmlDocument.HasElements)
{
XElement xElement = new XElement(xmlDocument.Name.LocalName);
xElement.Value = xmlDocument.Value;
return xElement;
}
return new XElement(xmlDocument.Name.LocalName, xmlDocument.Elements().Select(el => RemoveAllNamespaces(el)));
}
孟子E章 2010-10-15
  • 打赏
  • 举报
回复
你需要使用
XmlNamespaceManager
宝_爸 2010-10-15
  • 打赏
  • 举报
回复
宝_爸 2010-10-15
  • 打赏
  • 举报
回复
xmlns是namespace,不是一般的属性。
wapdos 2010-10-15
  • 打赏
  • 举报
回复
例如


string strXml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" +
"<WebServiceSync xmlns=\"http://www.ctzj.com/websrvc/xmlBean\">" +
//"<WebServiceSync>" +
"<WebGISSync>" +
"<WebGISSyncRequest>" +
"<IntegrationID>资产Id</IntegrationID>" +
"<RelaIntegrationID>关联关系(关联资产Id)</RelaIntegrationID>" +
"<OrderHeaderId>订单头Id</OrderHeaderId>" +
"<CRMItemID>行项目Id</CRMItemID>" +
"<ProdCode>产品类别</ProdCode>" +
"<RelationCode>动作</RelationCode>" +
"<MethodName>方法名</MethodName>" +
"<ActionType>子行为</ActionType>" +
"<PreNumber>老号码</PreNumber>" +
"<StdAddrId>最终地址ID</StdAddrId>" +
"<ActionCode>动作类型</ActionCode>" +
"<AreaCode>营业区</AreaCode>" +
"</WebGISSyncRequest>" +
"</WebGISSync>" +
"</WebServiceSync>";
string ss = "xmlns=\"http://www.ctzj.com/websrvc/xmlBean\"";
strXml = strXml.Replace(ss,"");
if (strXml != "")
{
XmlDocument XmlDoc = new XmlDocument();
XmlDoc.LoadXml(strXml);
XmlNode root = XmlDoc.DocumentElement;
string RelaIntegrationID = root.SelectSingleNode("/WebServiceSync/WebGISSync/WebGISSyncRequest/RelaIntegrationID").InnerText;

}




wapdos 2010-10-15
  • 打赏
  • 举报
回复
貌似XML 根节点有属性的时候,要把地址替换掉,否则不能用路径
wuyq11 2010-10-15
  • 打赏
  • 举报
回复
XmlDocument doc= new XmlDocument();
doc.Load("");
foreach (XmlNode node in doc.GetElementsByTagName("Hitem"))
{}
xundeng 2010-10-15
  • 打赏
  • 举报
回复
XML文件的根节点有太多的属性如下所示的 SCL节点.
 <?xml version="1.0" encoding="UTF-8"?>
<SCL xmlns="http://www.iec.ch/61850/2003/SCL" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance ./xsd/SCL.xsd" xsi:schemaLocation="http://www.iec.ch/61850/2003/SCL .\xsd\SCL.xsd" xmlns:ext="http://nari-relays.com">
<Header id="template" version="R6" revision="reversion" toolID="toolID" nameStructure="IEDName">
<History>
<Hitem version="1.0" revision="0.1" when="2010/10/13 19:35:28" why="update the file"></Hitem>
<Hitem version="1.0" revision="0.2" when="2010/10/14 09:00:07" why="update the file"></Hitem>
<Hitem version="1.0" revision="0.3" when="2010/10/14 13:28:49" why="update the file"></Hitem>
</History>
</Header>
<IED>....</IED>



用XmlDocument.Load时读不到根节点下面的子节点,有什么办法???
如果把根节点的属性都去掉变成

<?xml version="1.0" encoding="UTF-8"?>
<SCL>
<Header id="template" version="R6" revision="reversion" toolID="toolID" nameStructure="IEDName">
<History>
<Hitem version="1.0" revision="0.1" when="2010/10/13 19:35:28" why="update the file"></Hitem>
<Hitem version="1.0" revision="0.2" when="2010/10/14 09:00:07" why="update the file"></Hitem>
<Hitem version="1.0" revision="0.3" when="2010/10/14 13:28:49" why="update the file"></Hitem>
</History>
</Header>
<IED>....</IED>



就可以读到下面的子节点了.
现在的问题是XML文本不能修改.能不能修改XmlDocument的属性使它能支持N多属性的根节点.
 
OXmlDoct.Load(cidpath.Text);
NoList = OXmlDoct.DocumentElement.SelectNodes("IED");

读第一个XML时NoList.Count值为0
读第二个XML时NoList.Count值为1
????????????????????????????????
宝_爸 2010-10-15
  • 打赏
  • 举报
回复
两个是一样的。

估计你没有使用XmlNamespaceManager

参考下面帖子的回答:
http://topic.csdn.net/u/20101015/10/99ae60ab-7725-4bf5-94e8-bc3e07bc3227.html
sprc_lcl 2010-10-15
  • 打赏
  • 举报
回复
可以读啊

<?xml version="1.0" encoding="UTF-8"?>
<SCL xmlns="http://www.iec.ch/61850/2003/SCL" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance ./xsd/SCL.xsd" xsi:schemaLocation="http://www.iec.ch/61850/2003/SCL .\xsd\SCL.xsd" xmlns:ext="http://nari-relays.com">
<Header id="template" version="R6" revision="reversion" toolID="toolID" nameStructure="IEDName">
<History>
<Hitem version="1.0" revision="0.1" when="2010/10/13 19:35:28" why="update the file"></Hitem>
<Hitem version="1.0" revision="0.2" when="2010/10/14 09:00:07" why="update the file"></Hitem>
<Hitem version="1.0" revision="0.3" when="2010/10/14 13:28:49" why="update the file"></Hitem>
</History>
</Header>
</SCL>





XmlDocument xd = new XmlDocument();
xd.Load(@"D:\aaaaa.xml");
int hC = xd.GetElementsByTagName("Hitem").Count;
MessageBox.Show(hC.ToString());//3
wuyq11 2010-10-15
  • 打赏
  • 举报
回复
怎么读取的
Linq to xml
  • 打赏
  • 举报
回复
如果是固定几个属性的话利用XmlPath
sprc_lcl 2010-10-15
  • 打赏
  • 举报
回复
看不出你两个xml有啥区别

111,129

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Creator Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧