读取含有命名空间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;
        }
原文地址:https://www.cnblogs.com/Jessy/p/2803595.html