简体   繁体   English

我将如何解析自己的XML ..?

[英]How would I parse my own XML..?

I made this XML file that I need to try and generate a GUI from. 我制作了这个XML文件,需要尝试从中生成GUI。 No I am not going to jump over to WPF if you were wondering :-) 不,如果您想知道的话,我不会跳过WPF的:-)

Here is the XML that I made: 这是我制作的XML:

<?xml version="1.0" encoding="UTF-8"?>
<root>
    <gui groupboxlabel="Barnets Stamdata" type="CHILD">
        <textbox label="CPR" />
        <textbox label="Navn" />
        <textbox label="Efternavn" />
        <textbox label="Addresse" />
        <textbox label="Hus nr." />
        <textbox label="Opgang" />
        <textbox label="Post Nr." />
        <textbox label="By" />
        <textbox label="Email" />
        <textbox label="Telefon nr." />
        <textbox label="Sagsbehandler" />
        <textbox label="Konsulent" />
        <textbox label="Aflastning" />
        <!-- <combobox label="Foranstaltning" /> -->
        <!-- <date label="Anbring" /> -->
        <!-- <date label="Udskriv" /> -->
    </gui>
</root>

I need to find the gui tag first, so I can extract the 2 pieces of information there. 我需要首先找到gui标签,因此我可以在那里提取2条信息。 Then I have to make a custom textbox control with a certain label name for every textbox child there. 然后,我必须为那里的每个文本框子级创建一个具有特定标签名称的自定义文本框控件。

I tried to do something like this at firs to try and print out what it looked like, but the code doesn't work because the child nodes that I find are null: 我试图在第一时间做这样的事情来尝试打印出看起来像什么,但是代码不起作用,因为我发现的子节点为空:

public void CreateNewLayout(Form parent, String path, String token)
{
    XmlDocument xmlDoc = new XmlDocument();
    xmlDoc.Load(path);

    XmlNodeList gui = xmlDoc.GetElementsByTagName("gui");
    if (gui.Count == 0)
    {
        MessageBox.Show("XML fil har ingen elementer", "Fejl");
        return;
    }
    while (gui.GetEnumerator().MoveNext())
    {
        gui.GetEnumerator().Current.ToString();
    }
}

Problem is, my XML is pretty rusty...any help? 问题是,我的XML很生锈...有什么帮助吗?

You can use Linq-Xml 您可以使用Linq-Xml

var document = XDocument.Parse(inputXmlString);

document
.Root
.Element("gui")
.Elements()
.Select(element =>
   new
   {
       Type = element.Name,
       Label = element.Attribute("label").Value,
   })
.Dump();

This is a quick example in Linqpad to show how to turn your XML into an anonymous type. 这是Linqpad中的一个简单示例,展示了如何将XML转换为匿名类型。 It doesn't have to be an anonymous type it can be able type you want... 它不必是匿名类型,也可以是您想要的类型...

Also, if you want to filter elements, pass the element name into .Elements(string) . 另外,如果要过滤元素,请将元素名称传递给.Elements(string)

I would suggest giving different values for your textbox nodes such as textbox1, textbox2... If not something like the following should work: 我建议为您的文本框节点(例如textbox1,textbox2)提供不同的值。

    System.Xml.XmlDocument doc = new System.Xml.XmlDocument();
    List<string> labels = new List<string>();
    doc.LoadXml([This would be text from file]);
    string groupboxlabel = doc.SelectSingleNode("root/gui").Attributes["groupboxlabel"].Value;
    string type = doc.SelectSingleNode("root/gui/textbox").Attributes["type"].Value;
    System.XmlNodeList nodeList = doc.SelectNodes("root/gui");
    foreach (XmlNode node in nodeList)
    {
        labels.Add(node.Attributes["label"].Value;);//Now you will have a list of labels
    }

Hope this helps 希望这可以帮助

Use something like this: 使用这样的东西:

XDocument document = XDocument.Load(@"C:\DOTNET\PRACTICE\XmlTest\XmlTest\XMLFile1.xml");

XElement guiNode = document.Root.Element("gui");

List<XAttribute> attributes = new List<XAttribute>();

foreach(var attribute in guiNode.Attributes())
{
    attributes.Add(attribute);
}

This uses the XDocument API instead of the older XMLDocument API. 这将使用XDocument API而不是较早的XMLDocument API。 You can add null checks wherever you need them. 您可以在任何需要的地方添加空检查。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM