我需要构建像下面这样的XML并将其转换为字符串
XML中的值将动态变化。
任何建议?
<?xml version='1.0' encoding='utf-8'?>
<Message release='001' version='008' xmlns='http://www.ncpdp.org/schema/SCRIPT'>
<Header>
<To Qualifier='ZZZ'>xxxxxxx</To>
<From Qualifier='ZZZ'>xxxxxxx</From>
<MessageID>xxxxxx</MessageID>
<SentTime>2002-09-30T08:15:22</SentTime>
<Security>
<UsernameToken>
<Username/>
</UsernameToken>
<Sender>
<SecondaryIdentification>xxxxxxxxx</SecondaryIdentification>
</Sender>
<Receiver>
<SecondaryIdentification>xxxxxxxxxxx</SecondaryIdentification>
</Receiver>
</Security>
<TestMessage>1</TestMessage>
</Header>
<Body>
<RxHistoryRequest>
<RxReferenceNumber>POCDH1</RxReferenceNumber>
<Prescriber>
<Identification>
<NPI>xxxxxxx</NPI>
<DEANumber>xxxxxxxx</DEANumber>
</Identification>
<Name>
<LastName>JONES</LastName>
<FirstName>MARK</FirstName>
</Name>
<PhoneNumbers>
<Phone>
<Number>6152219800</Number>
<Qualifier>TE</Qualifier>
</Phone>
</PhoneNumbers>
</Prescriber>
<Patient>
<PatientRelationship>1</PatientRelationship>
<Name>
<LastName>PALTROW</LastName>
<FirstName>BRUCE</FirstName>
<MiddleName>K</MiddleName>
</Name>
<Gender>M</Gender>
<DateOfBirth>1954-12-25</DateOfBirth>
</Patient>
<BenefitsCoordination>
<PayerIdentification>
<PayerID>PBM123</PayerID>
</PayerIdentification>
<PayerName>INSURANCE COMPANY NAME</PayerName>
<CardholderID>MEMBERID</CardholderID>
<CardholderName>PALTROW, BRUCE</CardholderName>
<GroupID>B000000</GroupID>
<Consent>Y</Consent>
<PBMMemberID>B000000%111111110%001</PBMMemberID>
</BenefitsCoordination>
</RxHistoryRequest>
</Body>
</Message>
我试过这样的事情
创建可序列化的类
[Serializable]
[XmlRoot("Message", Namespace = "http://www.domain.com/messaging")]
public class RxHistoryRequest
{
[XmlAttribute]
public string version = string.Empty;
[XmlAttribute]
public string release = string.Empty;
[XmlAttribute("schemaLocation", Namespace = XmlSchema.InstanceNamespace)]
public string xsiSchemaLocation = ConfigurationManager.AppSettings["Namespace"] + " " +
ConfigurationManager.AppSettings["SS_DirectorySchema"];
public Header Header = new Header();
public RxHistory Body = new RxHistory();
}
//rest of the Properties in the classes
在我的方法中
RxHistoryRequest RxHReq = new RxHistoryRequest();
RxHReq.version = "008"; //ConfigurationManager.AppSettings["SSversion"];
RxHReq.release = "001"; //ConfigurationManager.AppSettings["SSrelease"];
//declare values to the rest of the Properties
//then
MemoryStream memoryStream = new MemoryStream();
XmlTextWriter xmlTextWriter = new XmlTextWriter(memoryStream, Encoding.UTF8);
XmlSerializer xs = new XmlSerializer(typeof(RxHistoryRequest));
xs.Serialize(xmlTextWriter, RxHReq);
XmlSerializerNamespaces ns = new XmlSerializerNamespaces();
ns.Add("messaging","http://www.ncpdp.org/schema/SCRIPT");
//ConfigurationManager.AppSettings["SurescriptsNamespace"]
memoryStream = (MemoryStream)xmlTextWriter.BaseStream;
ASCIIEncoding encoding = new ASCIIEncoding();
byte[] data = memoryStream.ToArray();
有些地方正在我需要的地方向右转。
答案 0 :(得分:0)
在@JonSkeet的建议下,我将我的想法转移到了Linq-to-XML 这让我的工作变得非常轻松。
如果有人有兴趣知道在Linq-to-XML上工作, 这是我的代码。
XNamespace ns = "http://www.ncpdp.org/schema/SCRIPT";
XElement xml = new XElement(ns + "Message",
new XAttribute("release", "001"),
new XAttribute("version", "008"),
new XElement(ns + "Header",
new XElement(ns + "To",
new XAttribute("Qualifier", "ZZZ"), "xxxxxxxxxx"),
new XElement(ns + "From",
new XAttribute("Qualifier", "ZZZ"), "xxxxxxxxxxxx"),
new XElement(ns + "MessageID", CommonController.GetIntegerGUID().ToString()),
new XElement(ns + "SentTime", "2002-09-30T08:15:22"),
new XElement(ns + "Security",
new XElement(ns + "UsernameToken",
new XElement(ns + "Username", "")),
new XElement(ns + "Sender",
new XElement(ns + "SecondaryIdentification", "PASSWORD")),
new XElement(ns + "Receiver",
new XElement(ns + "SecondaryIdentification", "RXHUB"))
),
new XElement(ns + "TestMessage", "1")
),
new XElement(ns + "Body",
new XElement(ns + "RxHistoryRequest",
new XElement(ns + "RxReferenceNumber", "POCDH1"),
// it goes on like that
希望这对某些人有用。