读取含有命名空间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 Hinesname>
      <phone type="home">206-555-0144phone>
      <phone type="work">425-555-0145phone>
      <address>
         <street1>123 Main Ststreet1>
         <city>Mercer Islandcity>
         <state>WAstate>
         <postal>68042postal>
      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 Dictionarystring, string>, KeyValuePair<string, string>> RetriveDataFromXmLFile(string xmlFilePath, out KeyValuePair<string, string> guidComment, out KeyValuePair<string, string> guidDescription)
        {
            // XML format
            //http://www.w3.org/2001/XMLSchema-instance">
            //
            //
            //EvaluationRule 
            //Update 
            //1055 
            //
            //1055 
            //AddPI_ACHCHECK_USs 
            //
            //
            //
            //TestByBob3 
            //TestByBob3 
            //86b6d584-2fb9-45fe-aea7-acc4479a3b3f 
            //

            Dictionarystring, string>, KeyValuePair<string, string>> groupsChange = new Dictionarystring, 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;
        }

 

你可能感兴趣的:(读取含有命名空间xml文件内容)