使用linq

时间:2016-08-01 11:02:41

标签: c# xml linq xml-parsing

这是我的xml代码

<?xml version="1.0" encoding="utf-8"?>
 <Event xmlns="">
<System>
  <Provider Name="" /> 
  <EventID>4624</EventID> 
  <Version>0</Version> 
  <Level>0</Level> 
  <Task>12544</Task> 
  <Opcode>0</Opcode> 
  <Keywords>0x0000</Keywords> 
  <TimeCreated SystemTime="20Z" /> 
  <EventRecordID>911588</EventRecordID> 
  <Correlation /> 
  <Execution ProcessID="608" ThreadID="704" /> 
  <Channel>Security</Channel> 
  <Computer></Computer> 
  <Security /> 
  </System>
 <EventData>
  <Data Name="SubjectUserSid">S-1-0-0</Data> 
  <Data Name="SubjectUserName">-</Data> 
  <Data Name="SubjectDomainName">-</Data> 
  <Data Name="SubjectLogonId">0x0</Data> 
  <Data Name="TargetUserSid">S1937-12707</Data> 
  <Data Name="TargetUserName"></Data> 

  </EventData>
  </Event>

我希望得到Name所有Data节点的 XmlDocument xmlDoc = new XmlDocument(); try { xmlDoc.Load("C:\\Users\\bk.koc\\Desktop\\Gelenxml.xml"); foreach (XmlNodeList node in xmlDoc.GetElementsByTagName("Data")) { MessageBox.Show(node.Attributes["Name"].Value); MessageBox.Show(node["Name"].Value); } 属性值和元素值

我试过了,

 myWebView.getSettings().setJavaScriptEnabled(true);
            myWebView.loadUrl("https://google.com/");
            WebSettings webSettings = myWebView.getSettings();
            webSettings.setJavaScriptEnabled(true);
            //improve webView performance
            myWebView.getSettings().setRenderPriority(WebSettings.RenderPriority.HIGH);
            myWebView.getSettings().setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);
            myWebView.getSettings().setAppCacheEnabled(true);
            myWebView.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);
            webSettings.setDomStorageEnabled(true);
            webSettings.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.NARROW_COLUMNS);
            webSettings.setUseWideViewPort(true);
            webSettings.setSavePassword(true);
            webSettings.setSaveFormData(true);
            webSettings.setEnableSmoothTransition(true);
            myWebView.setWebViewClient(new WebViewClient());

case R.id.menu_item_share: {
                Intent sharingIntent = new Intent(android.content.Intent.ACTION_SEND);
                sharingIntent.setType("text/plain");
                sharingIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "Subject Here");
                sharingIntent.putExtra(android.content.Intent.EXTRA_TEXT, shareBody);
                startActivity(Intent.createChooser(sharingIntent, getResources().getString(R.string.menu_item_share)));
                break;

我得到了所有的名字,但这给了我一个错误。

  

类型'System.NullReferenceException'的未处理异常   发生在WindowsFormsApplication4.exe

中      

附加信息:对象引用未设置为的实例   对象

如何修复

2 个答案:

答案 0 :(得分:0)

你可以通过这个

XDocument doc = XDocument.Load(@"C:\Users\bk.koc\Desktop\Gelenxml.xml");
var a = from m in doc.Descendants("EventData").Elements("Data")
         select new
          {
            Name = m.Attribute("Name").Value,
            Value = m.Value

          };

然后

foreach (var v in a)
   {
       MessageBox.Show(v.Name);
       MessageBox.Show(v.Value);

   }

<强>更新 我用过这个xml

<?xml version="1.0" encoding="utf-8"?>
<EventData>
  <Data Name="SubjectUserSid">S-1-0-0</Data> 
  <Data Name="SubjectUserName">-</Data> 
  <Data Name="SubjectDomainName">-</Data> 
  <Data Name="SubjectLogonId">0x0</Data>
</EventData>

答案 1 :(得分:0)

node.Attributes["Name"]

返回正确的XmlAttribute,您可以访问该Value个实例的XmlAttribute属性。

node["Name"]

尝试返回名为"Name"的第一个子元素,但由于您的Data标记没有元素,{{1 }}返回node["Name"]

因此,当您尝试访问null的{​​{1}}属性时,会引发Value