读取含有命名空间xml文件内容

.NET Language-Integrated Query for XML Data

http://msdn.microsoft.com/library/bb308960.aspx 

XNamespace myNs = "http://mycompany.com";

XElement contacts =
   new XElement(myNs + "contacts",
      new XElement(myNs + "contact",
         new XElement(myNs + "name", "Patrick Hines"),
         new XElement(myNs + "phone", "206-555-0144", 
             new XAttribute("type", "home")),
         new XElement(myNs + "phone", "425-555-0145",
             new XAttribute("type", "work")),
         new XElement(myNs + "address",
            new XElement(myNs + "street1", "123 Main St"),
            new XElement(myNs + "city", "Mercer Island"),
            new XElement(myNs + "state", "WA"),
            new XElement(myNs + "postal", "68042")
         )
      )
   );

 

产生如下文件:在最上层指定命名空间,分节点默认还有父节点的命名空间*继承性)

<contacts xmlns="http://mycompany.com">
   <contact>
      <name>Patrick Hines</name>
      <phone type="home">206-555-0144</phone>
      <phone type="work">425-555-0145</phone>
      <address>
         <street1>123 Main St</street1>
         <city>Mercer Island</city>
         <state>WA</state>
         <postal>68042</postal>
      </address>
   </contact>
</contacts>  

 

 XML Namespaces

http://www.jclark.com/xml/xmlns.htm

http://en.wikipedia.org/wiki/XML_namespace

Code Project:

http://www.codeproject.com/Articles/30965/Read-XML-with-Namespace-resolution-using-XLinq-XEl

private Dictionary<KeyValuePair<string, string>, KeyValuePair<string, string>> RetriveDataFromXmLFile(string xmlFilePath, out KeyValuePair<string, string> guidComment, out KeyValuePair<string, string> guidDescription)
        {
            // XML format
            //<ConfigChangeGroup xmlns="urn:schemas.amc.com/Cdefce/Name/Mode/2011/04" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
            //<Changes>
            //<ConfigChange>
            //<ConfigObjectType>EvaluationRule</ConfigObjectType> 
            //<ConfigVerb>Update</ConfigVerb> 
            //<Key>1055</Key> 
            //<NewConfig i:type="EvaluationRule">
            //<RuleId>1055</RuleId> 
            //<Name>AddPI_ACHCHECK_USs</Name> 
            //</NewConfig>
            //</ConfigChange>
            //</Changes>
            //<Comments>TestByBob3</Comments> 
            //<Description>TestByBob3</Description> 
            //<GroupId>86b6d584-2fb9-45fe-aea7-acc4479a3b3f</GroupId> 
            //</ConfigChangeGroup>

            Dictionary<KeyValuePair<string, string>, KeyValuePair<string, string>> groupsChange = new Dictionary<KeyValuePair<string, string>, KeyValuePair<string, string>>();

            XNamespace urnl = "urn:schemas.amc.com/Cdefce/Name/Mode/2011/04";
            XNamespace i = "http://www.w3.org/2001/XMLSchema instance";

            XElement elem = XElement.Load(xmlFilePath);

            string changeGuid = elem.Element(urnl + "GroupId").Value;
            string comments = elem.Element(urnl + "Comments").Value;
            string description = elem.Element(urnl + "Description").Value;

            guidComment = new KeyValuePair<string, string>(changeGuid, comments);
            guidDescription = new KeyValuePair<string, string>(changeGuid, description);

            var configChanges = elem.Elements(urnl + "Changes").Elements(urnl + "ConfigChange");

            foreach (var change in configChanges)
            {
                string key = change.Element(urnl + "Key").Value;
                string configObjectType = change.Element(urnl + "ConfigObjectType").Value;
                string text = change.ToString();

                KeyValuePair<string, string> idXml = new KeyValuePair<string, string>(changeGuid, text);
                KeyValuePair<string, string> typeKey = new KeyValuePair<string, string>(configObjectType, key);

                groupsChange.Add(typeKey, idXml);
            }

            return groupsChange;
        }

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值