我需要幫助檢索第一個文件中的元素國家及其值1,第二個文件中的元素名稱及其值XYZ。Linq to xml [其中節點存在於一個文件中而不存在於另一個文件中]
我正在接受來自app.config的輸入,並且我存儲的這兩個文件都是C:\ Test \和文件。
sample.xml中
<?xml version="1.0" encoding="utf-8"?>
<env:Contentname xmlns:env="http://data.schemas" xmlns="http://2013-02-01/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<env:Body>
<env:Content action="Hello">
<env:Data xsi:type="Yellow">
</env:Data>
</env:Content>
<env:Content action="Hello">
<env:Data xsi:type="Red">
<Status xmlns="http://2010-10-10/">
<Id >39681</Id>
<Name>Published</Name>
</Status>
</env:Data>
</env:Content>
<env:Content action="Hello">
<env:Data xsi:type="green">
<Document>
<country>1</country>
</Document>
</env:Data>
</env:Content>
</env:Body>
</env:Contentname>
和我的第二個文件是Sample1.xml
<?xml version="1.0" encoding="utf-8"?>
<env:Contentname xmlns:env="http://data.schemas" xmlns="http://2013-02-01/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<env:Body>
<env:Content action="Hello">
<env:Data xsi:type="Yellow">
</env:Data>
</env:Content>
<env:Content action="Hello">
<env:Data xsi:type="Red">
<Status xmlns="http://2010-10-10/">
<Id >39681</Id>
<Name>Published</Name>
</Status>
</env:Data>
</env:Content>
<env:Content action="Hello">
<env:Data xsi:type="green">
<Document>
<Name>XYZ</Name>
</Document>
</env:Data>
</env:Content>
</env:Body>
</env:Contentname>
我想這一點,
using System;
using System.Collections.Generic;
using System.Xml.Linq;
using System.Linq;
using System.Configuration;
using System.IO;
namespace LinEx
{
class Program
{
static void Main(string[] args)
{
String ReadFile = System.Configuration.ConfigurationManager.AppSettings["Check"];
DirectoryInfo di = new DirectoryInfo(ReadFile);
FileInfo[] rgFiles = di.GetFiles("*.xml");
foreach (FileInfo fi in rgFiles)
{
XElement root = XElement.Load(fi.FullName);
XNamespace aw = "http://data.schemas";
XNamespace kw = "http://www.w3.org/2001/XMLSchema-instance";
XNamespace ns = "http://2013-02-01/";
var result = from x in root.Descendants(aw + "Content")
where (string)x.Attribute("action") == "Hello"
from y in x.Descendants(aw + "Data")
where (string)y.Attribute(kw + "type") == "green"
select new
{
Firstel = y.Element(ns + "Document").Element(ns + "country").Value,
Secondel=y.Element(ns+"Document").Element(ns+"Name").Value,
};
foreach (var item in result)
{
Console.WriteLine("Country value={0}", item.Firstel);
Console.WriteLine("Name value={0}", item.Secondel);
}
}
Console.ReadLine();
}
}
}
但我得到錯誤(對象引用未設置到一個對象的實例)。善良幫助我,並提前致謝。
什麼是期望的輸出?您是否想通過將它們連接在一起來修復XML,或者您想要實現什麼功能? – 2015-03-19 07:27:27
姓名元素不存在於第一個文件中,並且country元素不存在於第二個文件中,那麼我也得到了Country Value =和Name value =,如果不存在則意味着我不應該要求這些值,我做到了? – 2015-03-19 07:34:07
@Thomas Lindvall:我希望輸出像Country value = 1和Name value = XYZ。 – 2015-03-19 07:40:07