Using C#, how can we pull attribute values from an XML Schema file and output that onto a CSV file?

♀尐吖头ヾ 提交于 2019-12-20 06:31:04

问题


I am trying to pull the attribute values for each of the element, that is in this XMl Schema file.


回答1:


you can use System.Xml.Linq to get all the elements and the required attributes as below

        XDocument document = XDocument.Load(@"D:\New Text Document.xml");
        var eleCollection = document.Elements("element");

        foreach (var element in eleCollection)
        {
            var type = element.Attribute("Type").Value;
        }



回答2:


This is easiest way one can use for loops on nodes to get the information in each node. use node.ChildNodes property to get the chilenodes.

XmlDocument doc = new XmlDocument();
doc.Load("filepath");
//Here Path could be- "//ElementType" ---> this will give all nodes with name ElementType 
XmlNodeList nodes= doc.SelectNodes("//give path of nodes you want attributes for");
foreach (XmlNode node in nodes)
{
    //Assuming you want information of element tags 
    foreach (XmlNode child in node.ChildNodes)
    {
        string name= node.Attributes["type"].Value;
        string name= node.Attributes["label"].Value;
    }
}


来源:https://stackoverflow.com/questions/58178049/using-c-how-can-we-pull-attribute-values-from-an-xml-schema-file-and-output-th

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!