首页 新闻 搜索 专区 学院

求用C#语言查询一非常规xml文件代码

0
悬赏园豆:10 [已解决问题] 解决于 2011-12-21 09:45

文件内容如下:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>Product ID</key>
<string>IPHONE_1ND</string>
<key>CFBundleName</key>
<string>AlipayPortal</string>
<key>DTSDKName</key>
<string>iphoneos4.2</string>
<key>DTXcode</key>
<string>0325</string>
<key>UTImportedTypeDeclarations</key>
<array>
</array>
<key>Safepay version</key>
<string>2.2.4</string>
<key>CFBundleDevelopmentRegion</key>
<string>zh_CN</string>
<key>CFBundleVersion</key>
<string>3.1</string>
<key>NSMainNibFile</key>
<string>MainWindow</string>
<key>CFBundleDocumentTypes</key>
<array>
</array>
<key>CFBundlePackageType</key>
<string>APPL</string>
<key>CFBundleIconFile</key>
<string>icon.png</string>
<key>User Agent</key>
<string>ALIPAY_FOR_IOS</string>
<key>DTPlatformName</key>
<string>iphoneos</string>
<key>CFBundleSupportedPlatforms</key>
<array>
<string>iPhoneOS</string>
</array>
<key>CFBundleInfoDictionaryVersion</key>
<string>6.0</string>
<key>CFBundleExecutable</key>
<string>AlipayPortal</string>
<key>DTCompiler</key>
<string>com.apple.compilers.llvm.clang.1_0</string>
<key>UIDeviceFamily</key>
<array>
<integer>1</integer>
</array>
<key>CFBundleURLTypes</key>
<array>
<dict>
<key>CFBundleURLName</key>
<string></string>
<key>CFBundleURLSchemes</key>
<array>
<string>alipay</string>
</array>
</dict>
</array>
<key>CFBundleIdentifier</key>
<string>com.alipay.iphoneclient</string>
<key>CFBundleResourceSpecification</key>
<string>ResourceRules.plist</string>
<key>CFBundleSignature</key>
<string>????</string>
<key>UTExportedTypeDeclarations</key>
<array>
</array>
<key>LSRequiresIPhoneOS</key>
<true/>
<key>CFBundleIconFiles</key>
<array>
<string>icon.png</string>
<string>icon@2x.png</string>
</array>
<key>CFBundleDisplayName</key>
<string>支付宝</string>
<key>DTXcodeBuild</key>
<string>10M2423</string>
<key>DTPlatformVersion</key>
<string>4.2 Seed 2</string>
<key>MinimumOSVersion</key>
<string>3.0</string>
<key>DTPlatformBuild</key>
<string>8C134</string>
<key>Product Version</key>
<string>3.0.1.1025</string>
</dict>
</plist>

要求一、用C#查询CFBundleDisplayName的值,此例值为“支付宝”

要求二:用C#查询CFBundleIconFiles的值,此例值为icon.png、icon@2x.png

dreamsme的主页 dreamsme | 初学一级 | 园豆:173
提问于:2011-12-21 02:40
< >
分享
最佳答案
1
 System.Xml.XmlDocument xmlDoc = new System.Xml.XmlDocument();
xmlDoc.LoadXml(xml);
//要求一、用C#查询CFBundleDisplayName的值,此例值为“支付宝”
System.Xml.XmlNode CFBundleDisplayNameXn = xmlDoc.DocumentElement.SelectSingleNode("//key[text()=\"CFBundleDisplayName\"]");
if (CFBundleDisplayNameXn != null)
{
System.Xml.XmlNode CFBundleDisplayXn = CFBundleDisplayNameXn.NextSibling;
Console.WriteLine(CFBundleDisplayXn.InnerText);
}
//要求二:用C#查询CFBundleIconFiles的值,此例值为icon.png、icon@2x.png

System.Xml.XmlNode CFBundleIconFilesParentXn = xmlDoc.DocumentElement.SelectSingleNode("//key[text()=\"CFBundleIconFiles\"]");
if (CFBundleIconFilesParentXn != null)
{
System.Xml.XmlNode CFBundleIconFilesXn = CFBundleIconFilesParentXn.NextSibling;
if (CFBundleIconFilesXn != null)
{
System.Xml.XmlNodeList xnList = CFBundleIconFilesXn.ChildNodes;
if(xnList!=null&&xnList.Count>0)
{
foreach(System.Xml.XmlNode xn in xnList)
{
Console.WriteLine(xn.InnerText);
}
}

}
}
收获园豆:10
LCM | 大侠五级 |园豆:6876 | 2011-12-21 09:16

第二点也可以这样找:

 //要求二:用C#查询CFBundleIconFiles的值,此例值为icon.png、icon@2x.png

System.Xml.XmlNodeList xnList = xmlDoc.DocumentElement.SelectNodes("//key[text()=\"CFBundleIconFiles\"]/following-sibling::array/string");
if (xnList != null && xnList.Count > 0)
{
foreach (System.Xml.XmlNode xn in xnList)
{
Console.WriteLine(xn.InnerText);
}
}



LCM | 园豆:6876 (大侠五级) | 2011-12-21 09:32

感谢大大,成功解决。不过要将LoadXml ()改为Load (),否则会报“根级别上的数据无效”的错误。

dreamsme | 园豆:173 (初学一级) | 2011-12-21 09:45

@dreamsme: 我的是xml格式的字符串,才用的LoadXml ,你从文件读,当然是用load啦。解决就好。

LCM | 园豆:6876 (大侠五级) | 2011-12-21 10:42
其他回答(1)
0

看这个:

<?xml version="1.0" encoding="utf-8"?><Company><Department><Name>IT Department</Name><Manager>猪头三</Manager><Employees><Employee><IDcode="001">10001</ID><Name>西门庆</Name><Gender></Gender></Employee><Employee><IDcode="002">10202</ID><Name>潘金莲</Name><Gender></Gender></Employee></Employees></Department></Company>

需要取得 Name 为 “西门庆” 的 Employee 节点,用XPath实现如下:

XmlDocument xmlDoc =newXmlDocument();xmlDoc.Load( Path.Combine( Environment.CurrentDirectory, "demo.xml" ) );XmlNode emp = xmlDoc.SelectSingleNode( "/Company/Department/Employees/Employee[Name='西门庆']" );//emp 即为 定位到的 Employee 节点

需要取得 code 为 002 的 Employee 节点, 用 XPath 实现如下:

XmlDocument xmlDoc =newXmlDocument();xmlDoc.Load( Path.Combine( Environment.CurrentDirectory, "demo.xml" ) );XmlNode emp = xmlDoc.SelectSingleNode( "/Company/Department/Employees/Employee/ID[@code='002']/parent::node()" );

XPath 寻径简介

xml文件,是一种树状结构, XPath 是针对xml文件寻径的一种 pattern。以开头的xml数据为例,下面给出几个常用的情形:

  • 取得所有的 Employee
/Company/Department/Employees/Employee

XPath的开头是一个斜线(/)代表绝对路径

  • 取得所有的 Name,不分层次
//Name

XPath 以 // 开头表示不限层次的一种模式

  • 使用*匹配未知名称的元素(不能匹配未知层级)

      1. 取得所有的 Employee

/Company/Department/Employees/*

      2. 取得Department下,包含有 Employee 作为子节点的节点

/Company/Department/*/Employee
 
悟行 | 园豆:12346 (专家六级) | 2011-12-21 09:13
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册