我需要阅读一个XML文件,其中包含常规订购数据以及有关订购产品的详细信息。我将常规订单数据放置在dataGridView中。在第二个DGV中,他想输入选定顺序的数据,例如<positionitem>, <materialcode>, <materialcatalognumber>, <materialdescription>, <quantity>, <unit>
代表标签<DocId>
= 222。
我在Visual Studio Community 2017中创建代码
下面的代码从变量“ pobraneDane”和XML数据中读取订单数据。
XmlDocument doc = new XmlDocument();
doc.LoadXml(pobraneDane);
XmlNodeList DocId = doc.GetElementsByTagName("DocId");
XmlNodeList DocNoExternal = doc.GetElementsByTagName("DocNoExternal");
XmlNodeList DocNo = doc.GetElementsByTagName("DocNo");
for (int i = 0; i < doc.Count; i++)
{
dataGridView1.Rows.Add(DocId[i].InnerXml, DocNoExternal[i].InnerXml, DocNo[i].InnerXml);
}
<?xml version="1.0" encoding="ISO-8859-1"?>
<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope">
<soap:Header/>
<soap:Body>
<ZdDocListGetResponse xmlns="http://www.serwer.pl">
<ArrayZdDocListGetResult>
<Status>1</Status>
<ZdDocListGetResult>
<ZdListGetHeaderResult>
<DocId>222</DocId>
<DocNo>2019/a/008</DocNo>
<DateDoc>2019-01-10T00:00:00</DateDoc>
<DocStatus>W</DocStatus>
<DocAddressDelivery>Koluszki, ul Jasna 12 </DocAddressDelivery>
<DocWarehouse>abc</DocWarehouse>
<DateRealizationPlanned>2019-02-19T00:00:00</DateRealizationPlanned>
<ArrayZdListGetLinesResult>
<ZdListGetLinesResult>
<PositionItem>1</PositionItem>
<MaterialCode>23456</MaterialCode>
<MaterialCatalogNumber>10-20-30</MaterialCatalogNumber>
<MaterialDescription>Stolik nocny Prometeusz</MaterialDescription>
<Quantity>2.0000</Quantity>
<Unit>SZT</Unit>
</ZdListGetLinesResult>
<ZdListGetLinesResult>
<PositionItem>2</PositionItem>
<MaterialCode>386700</MaterialCode>
<MaterialCatalogNumber>10-20-40</MaterialCatalogNumber>
<MaterialDescription>Komoda Kasjopea</MaterialDescription>
<Quantity>1.0000</Quantity>
<Unit>SZT</Unit>
</ZdListGetLinesResult>
</ArrayZdListGetLinesResult>
<ArrayZdListGetNotesResult/>
</ZdListGetHeaderResult>
<ZdListGetHeaderResult>
<DocId>333</DocId>
<DocNo>2019/b/009</DocNo>
<DateDoc>2019-01-14T00:00:00</DateDoc>
<DocStatus>W</DocStatus>
<DocAddressDelivery>Pacanowo, kozia 2</DocAddressDelivery>
<DocWarehouse>def</DocWarehouse>
<DateRealizationPlanned>2019-02-27T00:00:00</DateRealizationPlanned>
<ArrayZdListGetLinesResult>
<ZdListGetLinesResult>
<PositionItem>1</PositionItem>
<MaterialCode>554433</MaterialCode>
<MaterialCatalogNumber>10-20-30</MaterialCatalogNumber>
<MaterialDescription>Stolik nocny Prometeusz </MaterialDescription>
<Quantity>1.0000</Quantity>
<Unit>SZT</Unit>
</ZdListGetLinesResult>
<ZdListGetLinesResult>
<PositionItem>2</PositionItem>
<MaterialCode>888999</MaterialCode>
<MaterialCatalogNumber>10-20-50</MaterialCatalogNumber>
<MaterialDescription>Szafa Viktoria </MaterialDescription>
<Quantity>1.0000</Quantity>
<Unit>SZT</Unit>
</ZdListGetLinesResult>
</ArrayZdListGetLinesResult>
<ArrayZdListGetNotesResult/>
</ZdListGetHeaderResult>
</ZdDocListGetResult>
</ArrayZdDocListGetResult>
</ZdDocListGetResponse>
</soap:Body>
</soap:Envelope>
我想问一个示例代码,该代码将提取所选订单的详细信息-订单号为:<DocId>
订单详细信息例如<MaterialCode>
预先感谢您的帮助。
答案 0 :(得分:0)
尝试使用xml linq
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Xml;
using System.Xml.Linq;
using System.IO;
namespace WindowsFormsApplication45
{
public partial class Form1 : Form
{
const string FILENAME = @"c:\temp\test.xml";
public Form1()
{
InitializeComponent();
string xml = File.ReadAllText(FILENAME);
XDocument doc = XDocument.Parse(xml);
XElement root = doc.Root;
XNamespace ns = root.Descendants().Where(x => x.Name.LocalName == "ZdListGetHeaderResult").FirstOrDefault().GetDefaultNamespace();
List<HeaderResult> results = root.Descendants(ns + "ZdListGetHeaderResult").Select(x => new HeaderResult()
{
DocId = (int)x.Element(ns + "DocId"),
DocNo = (string)x.Element(ns + "DocNo"),
DateDoc = (DateTime)x.Element(ns + "DateDoc"),
DocStatus = (string)x.Element(ns + "DocStatus"),
DocAddressDelivery = (string)x.Element(ns + "DocAddressDelivery"),
DocWarehouse = (string)x.Element(ns + "DocWarehouse"),
DateRealizationPlanned = (DateTime)x.Element(ns + "DateRealizationPlanned"),
lines = x.Descendants(ns + "ZdListGetLinesResult").Select(y => new LinesResult()
{
PositionItem = (int)y.Element(ns + "PositionItem"),
MaterialCode = (int)y.Element(ns + "MaterialCode"),
MaterialCatalogNumber = (string)y.Element(ns + "MaterialCatalogNumber"),
MaterialDescription = (string)y.Element(ns + "MaterialDescription"),
Quantity = (decimal)y.Element(ns + "Quantity"),
Unit = (string)y.Element(ns + "Unit"),
}).ToList()
}).ToList();
DataTable dt = new DataTable();
dt.Columns.Add("DocId", typeof(int));
dt.Columns.Add("DocINo", typeof(string));
dt.Columns.Add("DateDoc", typeof(DateTime));
dt.Columns.Add("DocStatus", typeof(string));
dt.Columns.Add("DocAddressDelivery", typeof(string));
dt.Columns.Add("DocWarehouse", typeof(string));
dt.Columns.Add("DateRealizationPlanned", typeof(DateTime));
dt.Columns.Add("PositionItem", typeof(int));
dt.Columns.Add("MaterialCode", typeof(int));
dt.Columns.Add("MaterialCatalogNumber", typeof(string));
dt.Columns.Add("MaterialDescription", typeof(string));
dt.Columns.Add("Quantity", typeof(decimal));
dt.Columns.Add("Unit", typeof(string));
foreach (HeaderResult result in results)
{
foreach (LinesResult line in result.lines)
{
dt.Rows.Add(new object[] {
result.DocId,
result.DocNo,
result.DateDoc,
result.DocStatus,
result.DocAddressDelivery,
result.DocWarehouse,
result.DateRealizationPlanned,
line.PositionItem,
line.MaterialCode,
line.MaterialCatalogNumber,
line.MaterialDescription,
line.Quantity,
line.Unit
});
}
}
dataGridView1.DataSource = dt;
DataTable dt2 = dt.AsEnumerable().Where(x => x.Field<int>("DocId") == 222).CopyToDataTable();
dataGridView2.DataSource = dt2;
}
}
public class HeaderResult
{
public int DocId { get; set; }
public string DocNo { get; set; }
public DateTime DateDoc { get; set; }
public string DocStatus { get; set; }
public string DocAddressDelivery { get; set; }
public string DocWarehouse { get; set; }
public DateTime DateRealizationPlanned { get; set; }
public List<LinesResult> lines { get; set; }
}
public class LinesResult
{
public int PositionItem { get; set; }
public int MaterialCode { get; set; }
public string MaterialCatalogNumber { get; set; }
public string MaterialDescription { get; set; }
public decimal Quantity { get; set; }
public string Unit { get; set; }
}
}