Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用C#和xml DOM提取xml数据_C#_Xml_Dom_Xml Parsing - Fatal编程技术网

使用C#和xml DOM提取xml数据

使用C#和xml DOM提取xml数据,c#,xml,dom,xml-parsing,C#,Xml,Dom,Xml Parsing,我有一个如下所示的xml: <dfs:dataFields> <d:REQUIREMENT_SPECIFICATION ProjectName="Test 1" ProjectWorkCode="909" FunctionDepartmentName="X department" BrandApplicableName="All" ProjectManagerName="" ProjectSponserName="" BackgroundDescription="o

我有一个如下所示的xml:

<dfs:dataFields>
<d:REQUIREMENT_SPECIFICATION ProjectName="Test 1" ProjectWorkCode="909"     
FunctionDepartmentName="X department" BrandApplicableName="All" ProjectManagerName="" 
ProjectSponserName="" BackgroundDescription="others and users use the Online tool   to&#xA;to add users" 
StepChangeGoalDescription="In 2011, the new service will be active" ServiceImpactedName="xy service" 
xdado:OJsZDA="0">
</d:REQUIREMENT_SPECIFICATION>
</dfs:dataFields>
var doc= XDocument.Load(xmlFile);

XNamespace dNs = "http://actual-d-namespace-uri";

foreach(var element in doc.Root.Elements(dNs + "REQUIREMENT_SPECIFICATION"))
{
    var attributes = element.Attributes()
                            .Select(a => string.Format("{0}: {1}", a.Name, a.Value));

    Console.WriteLine("Requirement Specification " + string.Join(" ", attributes));
}

你能给我指一下正确的方向吗

除了循环子元素外,还需要循环每个元素的属性。

除了循环子元素外,还需要循环每个元素的属性。

对于这种情况,我更喜欢使用XmlDocument。您可以定义在文档中加载xml并只返回根节点的方法,在主方法中只循环遍历节点的属性:

private void ProcessAndDumpXml()
{
    StreamReader xmlStream = new StreamReader("example1.xml");
    XmlNode root = GetRootNode(xmlStream);

    // process nodes
    // ...
}

private XmlNode GetRootNode(StreamReader streamReader)
{            
    XmlDocument xmlDocument = new XmlDocument();            
    XmlNamespaceManager nsmgr = new XmlNamespaceManager(new NameTable());
    nsmgr.AddNamespace("dfs", "schema1");
    nsmgr.AddNamespace("d", "schema1");
    nsmgr.AddNamespace("xdado", "schema1");
    XmlParserContext context = new XmlParserContext(null, nsmgr, null, XmlSpace.None);
    XmlReaderSettings xset = new XmlReaderSettings { ConformanceLevel = ConformanceLevel.Fragment };
    XmlReader rd = XmlReader.Create(streamReader, xset, context);
    xmlDocument.Load(rd);

    return xmlDocument.DocumentElement.FirstChild;
}

我更喜欢在这种情况下使用XmlDocument。您可以定义在文档中加载xml并只返回根节点的方法,在主方法中只循环遍历节点的属性:

private void ProcessAndDumpXml()
{
    StreamReader xmlStream = new StreamReader("example1.xml");
    XmlNode root = GetRootNode(xmlStream);

    // process nodes
    // ...
}

private XmlNode GetRootNode(StreamReader streamReader)
{            
    XmlDocument xmlDocument = new XmlDocument();            
    XmlNamespaceManager nsmgr = new XmlNamespaceManager(new NameTable());
    nsmgr.AddNamespace("dfs", "schema1");
    nsmgr.AddNamespace("d", "schema1");
    nsmgr.AddNamespace("xdado", "schema1");
    XmlParserContext context = new XmlParserContext(null, nsmgr, null, XmlSpace.None);
    XmlReaderSettings xset = new XmlReaderSettings { ConformanceLevel = ConformanceLevel.Fragment };
    XmlReader rd = XmlReader.Create(streamReader, xset, context);
    xmlDocument.Load(rd);

    return xmlDocument.DocumentElement.FirstChild;
}

您发布的文档不是有效的XML文档,因为它缺少命名空间规范。但假设存在名称空间,您可以使用LINQ to XML这样做:

<dfs:dataFields>
<d:REQUIREMENT_SPECIFICATION ProjectName="Test 1" ProjectWorkCode="909"     
FunctionDepartmentName="X department" BrandApplicableName="All" ProjectManagerName="" 
ProjectSponserName="" BackgroundDescription="others and users use the Online tool   to&#xA;to add users" 
StepChangeGoalDescription="In 2011, the new service will be active" ServiceImpactedName="xy service" 
xdado:OJsZDA="0">
</d:REQUIREMENT_SPECIFICATION>
</dfs:dataFields>
var doc= XDocument.Load(xmlFile);

XNamespace dNs = "http://actual-d-namespace-uri";

foreach(var element in doc.Root.Elements(dNs + "REQUIREMENT_SPECIFICATION"))
{
    var attributes = element.Attributes()
                            .Select(a => string.Format("{0}: {1}", a.Name, a.Value));

    Console.WriteLine("Requirement Specification " + string.Join(" ", attributes));
}

您发布的文档不是有效的XML文档,因为它缺少命名空间规范。但假设存在名称空间,您可以使用LINQ to XML这样做:

<dfs:dataFields>
<d:REQUIREMENT_SPECIFICATION ProjectName="Test 1" ProjectWorkCode="909"     
FunctionDepartmentName="X department" BrandApplicableName="All" ProjectManagerName="" 
ProjectSponserName="" BackgroundDescription="others and users use the Online tool   to&#xA;to add users" 
StepChangeGoalDescription="In 2011, the new service will be active" ServiceImpactedName="xy service" 
xdado:OJsZDA="0">
</d:REQUIREMENT_SPECIFICATION>
</dfs:dataFields>
var doc= XDocument.Load(xmlFile);

XNamespace dNs = "http://actual-d-namespace-uri";

foreach(var element in doc.Root.Elements(dNs + "REQUIREMENT_SPECIFICATION"))
{
    var attributes = element.Attributes()
                            .Select(a => string.Format("{0}: {1}", a.Name, a.Value));

    Console.WriteLine("Requirement Specification " + string.Join(" ", attributes));
}

非常感谢你。这解决了我的问题。我正在遍历所有属性并打印出attribute.Item(I).Name和attribute.Item(I).Value;谢谢你在这方面的帮助!别忘了添加空检查和另一个验证结构,祝你好运!非常感谢你。这解决了我的问题。我正在遍历所有属性并打印出attribute.Item(I).Name和attribute.Item(I).Value;谢谢你在这方面的帮助!别忘了添加空检查和另一个验证结构,祝你好运!